'FORGET' ordering semantics (vs unlink & NFS)
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.
The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?
c) Has this problem been hit by any other fuse users (with NFS or otherwise)?
Dave
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).
Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?
Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlink foo/bar.txt
rmdir foo
close(fd).
In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
Vivek
Quoted 7 lines
c) Has this problem been hit by any other fuse users (with NFS or otherwise)?
Dave
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 33 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
Quoted 8 lines
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Dave
Quoted 9 lines
Vivek
Quoted 7 lines
c) Has this problem been hit by any other fuse users (with NFS or otherwise)?
Dave
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 8 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
So what file descriptor is that? Is it because of O_PATH fd we have stashed away in lo_inode?
Will NFS keep .nfsXXXX file around even if O_PATH fd is open for unlinked file?
Vivek
Quoted 48 lines · Vivek Goyal
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 8 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Quoted 5 lines
So what file descriptor is that? Is it because of
O_PATHfd we have stashed away inlo_inode?Will NFS keep .nfsXXXX file around even if
O_PATHfd is open for unlinked file?
If this issue is due to O_PATH fd opened by virtiofsd, then I guess we will have look into introducing synchronous and use that instead when inode is being dropped (I guess in fuse_evict_inode()).
Vivek
Miklos Szeredi
maintainerQuoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 8 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Can't think of anything better than a synchronous forget. Compile
only tested patch attached.
Thanks,
Miklos
fs/fuse/dir.c +21 -0
@@ -373,6 +373,26 @@ static struct vfsmount *fuse_dentry_automount(struct path *path)return ERR_PTR (err); } +static void fuse_dentry_iput (struct dentry *dentry,struct inode *inode) +{ +if (!__lockref_is_dead (&dentry->d_lockref)) { +/* +* This is an unlink/rmdir and removing the last ref to the +* dentry. Use synchronous FORGET in case filesystem requests +* it. +* +* FIXME: This is racy! Two or more instances of +* fuse_dentry_iput() could be running concurrently (unlink of +* several aliases in different directories). +*/ +set_bit (FUSE_I_SYNC_FORGET , &get_fuse_inode (inode)->state); +iput (inode); +clear_bit (FUSE_I_SYNC_FORGET , &get_fuse_inode (inode)->state); + }else { +iput (inode); + } +} +const struct dentry_operations fuse_dentry_operations = { .d_revalidate = fuse_dentry_revalidate, .d_delete = fuse_dentry_delete,
@@ -381,6 +401,7 @@ const struct dentry_operations fuse_dentry_operations = { .d_release = fuse_dentry_release, #endif .d_automount = fuse_dentry_automount, + .d_iput = fuse_dentry_iput, }; const struct dentry_operations fuse_root_dentry_operations = {
fs/fuse/fuse_i.h +2 -0
@@ -174,6 +174,8 @@ enum {FUSE_I_SIZE_UNSTABLE ,/* Bad inode */ FUSE_I_BAD , +/* Synchronous forget requested */ + FUSE_I_SYNC_FORGET, };struct fuse_conn ;
fs/fuse/inode.c +27 -3
@@ -115,6 +115,26 @@ static void fuse_free_inode(struct inode *inode)kmem_cache_free (fuse_inode_cachep, fi); } +static void fuse_sync_forget (struct inode *inode) +{ +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_forget_in inarg; +FUSE_ARGS (args); + +memset (&inarg,0 ,sizeof (inarg)); + inarg.nlookup = fi->nlookup; + args.opcode =FUSE_SYNC_FORGET ; + args.nodeid = fi->nodeid; + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + args.force = true; + +fuse_simple_request (fm, &args); +/* ignore errors */ +} +static void fuse_evict_inode (struct inode *inode) {struct fuse_inode *fi =get_fuse_inode (inode);
@@ -127,9 +147,13 @@ static void fuse_evict_inode(struct inode *inode)if (FUSE_IS_DAX (inode))fuse_dax_inode_cleanup (inode);if (fi->nlookup) { -fuse_queue_forget (fc, fi->forget, fi->nodeid, - fi->nlookup); - fi->forget =NULL ; +if (test_bit (FUSE_I_SYNC_FORGET , &fi->state)) { +fuse_sync_forget (inode); + }else { +fuse_queue_forget (fc, fi->forget, fi->nodeid, + fi->nlookup); + fi->forget =NULL ; + } } }if (S_ISREG (inode->i_mode) && !fuse_is_bad (inode)) {
include/uapi/linux/fuse.h +1 -0
@@ -499,6 +499,7 @@ enum fuse_opcode {FUSE_COPY_FILE_RANGE =47 ,FUSE_SETUPMAPPING =48 ,FUSE_REMOVEMAPPING =49 , +FUSE_SYNC_FORGET =50 ,/* CUSE specific operations */ CUSE_INIT = 4096,
Quoted 69 lines · Miklos Szeredi
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 8 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Can't think of anything better than a synchronous forget. Compile
only tested patch attached.Thanks,
Miklosfs/fuse/dir.c +5
-0@@ -373,6 +373,26 @@ static struct vfsmount *fuse_dentry_automount(struct path *path)return ERR_PTR (err); } +static void fuse_dentry_iput (struct dentry *dentry,struct inode *inode) +{ +if (!__lockref_is_dead (&dentry->d_lockref)) { +/* +* This is an unlink/rmdir and removing the last ref to the
fs/fuse/dir.c static struct vfsmount *fuse_dentry_automount(struct path *path) · line 390
Quoted 10 lines
fs/fuse/dir.c +10
-0+* dentry. Use synchronous FORGET in case filesystem requests +* it. +* +* FIXME: This is racy! Two or more instances of +* fuse_dentry_iput() could be running concurrently (unlink of +* several aliases in different directories). +*/ +set_bit (FUSE_I_SYNC_FORGET , &get_fuse_inode (inode)->state); +iput (inode); +clear_bit (FUSE_I_SYNC_FORGET , &get_fuse_inode (inode)->state);
Hi Miklos,
Can above iput() be final reference on inode and free the inode? If yes, then it is not safe to call clear_bit() after iput()?
Vivek
Quoted 89 lines
fs/fuse/dir.c +6
-0+ }else { +iput (inode); + } +} +const struct dentry_operations fuse_dentry_operations = { .d_revalidate = fuse_dentry_revalidate, .d_delete = fuse_dentry_delete,@@ -381,6 +401,7 @@ const struct dentry_operations fuse_dentry_operations = { .d_release = fuse_dentry_release, #endif .d_automount = fuse_dentry_automount, + .d_iput = fuse_dentry_iput, }; const struct dentry_operations fuse_root_dentry_operations = {fs/fuse/fuse_i.h +2
-0@@ -174,6 +174,8 @@ enum {FUSE_I_SIZE_UNSTABLE ,/* Bad inode */ FUSE_I_BAD , +/* Synchronous forget requested */ + FUSE_I_SYNC_FORGET, };struct fuse_conn ;fs/fuse/inode.c +27
-3@@ -115,6 +115,26 @@ static void fuse_free_inode(struct inode *inode)kmem_cache_free (fuse_inode_cachep, fi); } +static void fuse_sync_forget (struct inode *inode) +{ +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_forget_in inarg; +FUSE_ARGS (args); + +memset (&inarg,0 ,sizeof (inarg)); + inarg.nlookup = fi->nlookup; + args.opcode =FUSE_SYNC_FORGET ; + args.nodeid = fi->nodeid; + args.in_numargs =1 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + args.force = true; + +fuse_simple_request (fm, &args); +/* ignore errors */ +} +static void fuse_evict_inode (struct inode *inode) {struct fuse_inode *fi =get_fuse_inode (inode);@@ -127,9 +147,13 @@ static void fuse_evict_inode(struct inode *inode)if (FUSE_IS_DAX (inode))fuse_dax_inode_cleanup (inode);if (fi->nlookup) { -fuse_queue_forget (fc, fi->forget, fi->nodeid, - fi->nlookup); - fi->forget =NULL ; +if (test_bit (FUSE_I_SYNC_FORGET , &fi->state)) { +fuse_sync_forget (inode); + }else { +fuse_queue_forget (fc, fi->forget, fi->nodeid, + fi->nlookup); + fi->forget =NULL ; + } } }if (S_ISREG (inode->i_mode) && !fuse_is_bad (inode)) {include/uapi/linux/fuse.h +1
-0@@ -499,6 +499,7 @@ enum fuse_opcode {FUSE_COPY_FILE_RANGE =47 ,FUSE_SETUPMAPPING =48 ,FUSE_REMOVEMAPPING =49 , +FUSE_SYNC_FORGET =50 ,/* CUSE specific operations */ CUSE_INIT = 4096,
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open O_PATH fd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.
If you store a FileHandle (name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2) is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.
Essentially, all the open fd gives you in pinning the underlying inode to cache and I am not sure at all this is what you want to achieve.
Note that if virtifs wants to provide "proper" NFS support, where NFS file handles
survive host reboot, you will need FUSE support for LOOKUP_HANDLE [1] and then you don't even have to keep InodeData around until FORGET, but IMO there is no reason to wait for LOOKUP_HANDLE before making the change above.
Thanks,
Amir.
Miklos Szeredi
maintainerQuoted 58 lines · Amir Goldstein
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open
O_PATHfd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.
Quoted 7 lines
If you store a FileHandle (
name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2)is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.
You are perfectly right that using file handles would solve a number of issues, one being too many open file descriptors.
The issue with open_by_handle_at(2) is that it needs CAP_DAC_READ_SEARCH in the initial user namespace. That currently makes it impossible to use in containers and such.
Not sure if there has been proposals for making open_by_handle_at(2) usable in user namespaces. The difficulty is in verifying that an open file would have been obtainable by path lookup by the given user.
Thanks,
Miklos
Quoted 68 lines · Miklos Szeredi
Quoted 58 lines · Amir Goldstein
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open
O_PATHfd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.Quoted 7 lines
If you store a FileHandle (
name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2)is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.
Quoted 6 lines
You are perfectly right that using file handles would solve a number of issues, one being too many open file descriptors.
The issue with
open_by_handle_at(2)is that it needsCAP_DAC_READ_SEARCHin the initial user namespace. That currently makes it impossible to use in containers and such.
Is that a problem for virtiofsd? does it also run inside a container??
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement open_child_by_handle_at(int parent_fd, ...):
- readdir(parend_fd)
- search a match for d_ino
- name_to_handle_at() and verify match to stored filehandle
This is essentially what open_by_handle_at(2) does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part. And "connectable" file handles are used by nfsd to enforce "subtree_check" to make sure that file wasn't moved outside obtainable path after initial lookup.
Quoted 3 lines
Not sure if there has been proposals for making
open_by_handle_at(2)usable in user namespaces.
I don't remember seeing any.
Quoted 2 lines
The difficulty is in verifying that an
open file would have been obtainable by path lookup by the given user.
I think it can be allowed for user with CAP_DAC_READ_SEARCH in userns for FS_USERNS_MOUNT mounted in that userns.
Thanks,
Amir.
Quoted 82 lines · Amir Goldstein
Quoted 68 lines · Miklos Szeredi
Quoted 58 lines · Amir Goldstein
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open
O_PATHfd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.Quoted 7 lines
If you store a FileHandle (
name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2)is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.Quoted 6 lines
You are perfectly right that using file handles would solve a number of issues, one being too many open file descriptors.
The issue with
open_by_handle_at(2)is that it needsCAP_DAC_READ_SEARCHin the initial user namespace. That currently makes it impossible to use in containers and such.Is that a problem for virtiofsd? does it also run inside a container??
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
Quoted 6 lines
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement
open_child_by_handle_at(int parent_fd, ...):
-readdir(parend_fd)
- search a match ford_ino
-name_to_handle_at()and verify match to stored filehandle
That would have to be AT_EMPTY_PATH after opening the file by name.
Quoted 4 lines
This is essentially what
open_by_handle_at(2)does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part.
I meant this is what's done under the covers for the non cached inode case of "connectable" non-dir filehandles. Of course we cannot do that on every lookup.
What I forgot to say is that with this method we can close file fds if we have too many open files or on unlink() and open the file again in this method
if needed.
Thanks,
Amir.
Miklos Szeredi
maintainerQuoted 2 lines · Amir Goldstein
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
Okay.
Quoted 7 lines
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement
open_child_by_handle_at(int parent_fd, ...):
-readdir(parend_fd)
- search a match ford_ino
-name_to_handle_at()and verify match to stored filehandle
Quoted 5 lines
This is essentially what
open_by_handle_at(2)does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part. And "connectable" file handles are used by nfsd to enforce "subtree_check" to make sure that file wasn't moved outside obtainable path after initial lookup.
Yes, sort of makes sense, but will have corner cases for hard links in different directories, open files after unlink, etc..
Also back to the original problem: what we really want is to close the O_PATH descriptor on unlink(). This should be possible, regardless of any FORGET, assuming
1) no open files exist that reference the inode
2) no aliases have been looked up (i.e. just one cached dentry)
The attached untested patch tries to do this.
Thanks,
Miklos
tools/virtiofsd/passthrough_ll.c +44 -8
@@ -107,6 +107,11 @@ struct lo_inode {*/ gint refcount; +/* +* Number of open instances +*/ +gint opencount; +struct lo_key key;/*
@@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2; + +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));
@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;
@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }unref_inode_lolocked (lo, inode,1 );lo_inode_put (lo, &inode); }
@@ -1904,25 +1926,30 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)ssize_t fh;char buf[64 ];struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino); +int err =EBADF ; + +if (!inode) + goto out_err;fuse_log (FUSE_LOG_DEBUG ,"lo_open(ino=%" PRIu64", flags=%d)\n" , ino, fi->flags);update_open_flags (lo->writeback, lo->allow_direct_io, fi); -sprintf (buf,"%i" ,lo_fd (req, ino)); +sprintf (buf,"%i" , inode->fd); fd =openat (lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW ); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); - } + err = errno; +if (fd ==-1 ) + goto out_err;pthread_mutex_lock (&lo->mutex); fh =lo_add_fd_mapping (req, fd);pthread_mutex_unlock (&lo->mutex);if (fh ==-1 ) {close (fd); -fuse_reply_err (req,ENOMEM ); -return ; + err =ENOMEM ; + goto out_err; } fi->fh = fh;
@@ -1931,18 +1958,26 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) }else if (lo->cache ==CACHE_ALWAYS ) { fi->keep_cache =1 ; } +g_atomic_int_inc (&inode->opencount); +lo_inode_put (lo, &inode);fuse_reply_open (req, fi); +return ; + +out_err: +lo_inode_put (lo, &inode); +fuse_reply_err (req, err); }static void lo_release (fuse_req_t req,fuse_ino_t ino,struct fuse_file_info *fi) {struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino);struct lo_map_elem *elem;int fd =-1 ; - (void )ino; - +if (inode) +g_atomic_int_dec_and_test (&inode->opencount);pthread_mutex_lock (&lo->mutex); elem =lo_map_get (&lo->fd_map, fi->fh);if (elem) {
@@ -1951,6 +1986,7 @@ static void lo_release(fuse_req_t req, fuse_ino_t ino,lo_map_remove (&lo->fd_map, fi->fh); }pthread_mutex_unlock (&lo->mutex); +lo_inode_put (lo, &inode);close (fd);fuse_reply_err (req,0 );
Quoted 57 lines · Miklos Szeredi
Quoted 2 lines · Amir Goldstein
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
Okay.
Quoted 7 lines
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement
open_child_by_handle_at(int parent_fd, ...):
-readdir(parend_fd)
- search a match ford_ino
-name_to_handle_at()and verify match to stored filehandleQuoted 5 lines
This is essentially what
open_by_handle_at(2)does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part. And "connectable" file handles are used by nfsd to enforce "subtree_check" to make sure that file wasn't moved outside obtainable path after initial lookup.Yes, sort of makes sense, but will have corner cases for hard links in different directories, open files after unlink, etc..
Also back to the original problem: what we really want is to close the
O_PATHdescriptor onunlink(). This should be possible, regardless of any FORGET, assuming1) no open files exist that reference the inode
2) no aliases have been looked up (i.e. just one cached dentry)The attached untested patch tries to do this.
Thanks,
Miklostools/virtiofsd/passthrough_ll.c +7
-0@@ -107,6 +107,11 @@ struct lo_inode {*/ gint refcount; +/* +* Number of open instances +*/ +gint opencount; +struct lo_key key;/* @@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2;
tools/virtiofsd/passthrough_ll.c static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, · line 920
Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10
-0+ +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +
Hi Miklos,
So if I have a hard links to a file in a separate directories, then this path can hit. (Say dir1/file.txt and dir2/file-link.txt). IIUC, we will disable automatic inode->fd closing on these hardlinked files. That means this solution will not solve problem at hand for hard linked files. Am I missing something.
Quoted 12 lines
tools/virtiofsd/passthrough_ll.c +1
-0close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;
tools/virtiofsd/passthrough_ll.c static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) · line 1319
Quoted 7 lines
tools/virtiofsd/passthrough_ll.c +4
-0@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }
Can this be racy w.r.t lo_lookup(). IOW, say dir1/file.txt is being unlinked and we closed inode->fd. And before we could execute unref_inode_lolocked(), another parallel lookup of dir2/file-link.txt happens if it gets lo->muxtex lock first, it can still find this inode and bump up reference count. And that means lo_unlink() will not free inode (but close inode->fd) and now we will use an inode with closed O_PATH fd which lead to other failures later.
Thanks
Vivek
Quoted 77 lines
tools/virtiofsd/passthrough_ll.c +22
-8unref_inode_lolocked (lo, inode,1 );lo_inode_put (lo, &inode); }@@ -1904,25 +1926,30 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)ssize_t fh;char buf[64 ];struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino); +int err =EBADF ; + +if (!inode) + goto out_err;fuse_log (FUSE_LOG_DEBUG ,"lo_open(ino=%" PRIu64", flags=%d)\n" , ino, fi->flags);update_open_flags (lo->writeback, lo->allow_direct_io, fi); -sprintf (buf,"%i" ,lo_fd (req, ino)); +sprintf (buf,"%i" , inode->fd); fd =openat (lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW ); -if (fd ==-1 ) { -return (void )fuse_reply_err (req, errno); - } + err = errno; +if (fd ==-1 ) + goto out_err;pthread_mutex_lock (&lo->mutex); fh =lo_add_fd_mapping (req, fd);pthread_mutex_unlock (&lo->mutex);if (fh ==-1 ) {close (fd); -fuse_reply_err (req,ENOMEM ); -return ; + err =ENOMEM ; + goto out_err; } fi->fh = fh;@@ -1931,18 +1958,26 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) }else if (lo->cache ==CACHE_ALWAYS ) { fi->keep_cache =1 ; } +g_atomic_int_inc (&inode->opencount); +lo_inode_put (lo, &inode);fuse_reply_open (req, fi); +return ; + +out_err: +lo_inode_put (lo, &inode); +fuse_reply_err (req, err); }static void lo_release (fuse_req_t req,fuse_ino_t ino,struct fuse_file_info *fi) {struct lo_data *lo =lo_data (req); +struct lo_inode *inode =lo_inode (req, ino);struct lo_map_elem *elem;int fd =-1 ; - (void )ino; - +if (inode) +g_atomic_int_dec_and_test (&inode->opencount);pthread_mutex_lock (&lo->mutex); elem =lo_map_get (&lo->fd_map, fi->fh);if (elem) {@@ -1951,6 +1986,7 @@ static void lo_release(fuse_req_t req, fuse_ino_t ino,lo_map_remove (&lo->fd_map, fi->fh); }pthread_mutex_unlock (&lo->mutex); +lo_inode_put (lo, &inode);close (fd);fuse_reply_err (req,0 );
Miklos Szeredi
maintainerQuoted 22 lines · Vivek Goyal
Quoted 6 lines · Miklos Szeredi
tools/virtiofsd/passthrough_ll.c +2
-0@@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2;tools/virtiofsd/passthrough_ll.c static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, · line 920
Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10
-0+ +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +Hi Miklos,
Quoted 5 lines
So if I have a hard links to a file in a separate directories, then this path can hit. (Say
dir1/file.txtanddir2/file-link.txt). IIUC, we will disable automaticinode->fdclosing on these hardlinked files. That means this solution will not solve problem at hand for hard linked files. Am I missing something.
You are correct, this is a limited solution. The one above is a corner case, and probably not worth worrying about too much. However it can be fixed by having separate O_PATH fd's open for each alias (or at least for each alias that resides in a distinct directory).
NB: doing a synchronous FORGET on unlink does not solve this case either, since the inode will not receive a FORGET until the last alias is dropped.
Quoted 14 lines
Quoted 14 lines
tools/virtiofsd/passthrough_ll.c +1
-0close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );
Quoted 13 lines
tools/virtiofsd/passthrough_ll.c static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) · line 1319
Quoted 5 lines
tools/virtiofsd/passthrough_ll.c +4
-0fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }Can this be racy w.r.t
lo_lookup(). IOW, saydir1/file.txtis being unlinked and we closedinode->fd. And before we could executeunref_inode_lolocked(), another parallel lookup ofdir2/file-link.txthappens if it getslo->muxtexlock first, it can still find this inode and bump up reference count. And that meanslo_unlink()will not free inode (but closeinode->fd) and now we will use an inode with closedO_PATHfd which lead to other failures later.
Yes, that's a bug. Also needs serialization against all access to inode->fd.
Thanks,
Miklos
Quoted 67 lines · Miklos Szeredi
Quoted 22 lines · Vivek Goyal
Quoted 6 lines · Miklos Szeredi
tools/virtiofsd/passthrough_ll.c +2
-0@@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2;tools/virtiofsd/passthrough_ll.c static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, · line 920
Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10
-0+ +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +Hi Miklos,
Quoted 5 lines
So if I have a hard links to a file in a separate directories, then this path can hit. (Say
dir1/file.txtanddir2/file-link.txt). IIUC, we will disable automaticinode->fdclosing on these hardlinked files. That means this solution will not solve problem at hand for hard linked files. Am I missing something.You are correct, this is a limited solution. The one above is a corner case, and probably not worth worrying about too much. However it can be fixed by having separate
O_PATHfd's open for each alias (or at least for each alias that resides in a distinct directory).NB: doing a synchronous FORGET on unlink does not solve this case either, since the inode will not receive a FORGET until the last alias is dropped.
Quoted 14 lines
Quoted 14 lines
tools/virtiofsd/passthrough_ll.c +1
-0close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );Quoted 13 lines
tools/virtiofsd/passthrough_ll.c static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) · line 1319
Quoted 5 lines
tools/virtiofsd/passthrough_ll.c +4
-0fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }Can this be racy w.r.t
lo_lookup(). IOW, saydir1/file.txtis being unlinked and we closedinode->fd. And before we could executeunref_inode_lolocked(), another parallel lookup ofdir2/file-link.txthappens if it getslo->muxtexlock first, it can still find this inode and bump up reference count. And that meanslo_unlink()will not free inode (but closeinode->fd) and now we will use an inode with closedO_PATHfd which lead to other failures later.
Quoted 2 lines
Yes, that's a bug. Also needs serialization against all access to
inode->fd.
Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the FUSE_PASSTHROUGH threads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.
Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
David,
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
Anyone has other thoughts about how to reduce fragmentation in implementations?
Thanks,
Amir.
Quoted 97 lines · * Amir Goldstein ([email protected]) wrote:
Quoted 67 lines · Miklos Szeredi
Quoted 22 lines · Vivek Goyal
Quoted 6 lines · Miklos Szeredi
tools/virtiofsd/passthrough_ll.c +2
-0@@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2;tools/virtiofsd/passthrough_ll.c static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, · line 920
Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10
-0+ +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +Hi Miklos,
Quoted 5 lines
So if I have a hard links to a file in a separate directories, then this path can hit. (Say
dir1/file.txtanddir2/file-link.txt). IIUC, we will disable automaticinode->fdclosing on these hardlinked files. That means this solution will not solve problem at hand for hard linked files. Am I missing something.You are correct, this is a limited solution. The one above is a corner case, and probably not worth worrying about too much. However it can be fixed by having separate
O_PATHfd's open for each alias (or at least for each alias that resides in a distinct directory).NB: doing a synchronous FORGET on unlink does not solve this case either, since the inode will not receive a FORGET until the last alias is dropped.
Quoted 14 lines
Quoted 14 lines
tools/virtiofsd/passthrough_ll.c +1
-0close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );Quoted 13 lines
tools/virtiofsd/passthrough_ll.c static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) · line 1319
Quoted 5 lines
tools/virtiofsd/passthrough_ll.c +4
-0fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }Can this be racy w.r.t
lo_lookup(). IOW, saydir1/file.txtis being unlinked and we closedinode->fd. And before we could executeunref_inode_lolocked(), another parallel lookup ofdir2/file-link.txthappens if it getslo->muxtexlock first, it can still find this inode and bump up reference count. And that meanslo_unlink()will not free inode (but closeinode->fd) and now we will use an inode with closedO_PATHfd which lead to other failures later.Quoted 2 lines
Yes, that's a bug. Also needs serialization against all access to
inode->fd.Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the
FUSE_PASSTHROUGHthreads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
Quoted 7 lines
David,
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
The C version is the current main one, but the Rust version is catching up fast and I hope it will displace the C one sometime this year. Sergio owns the Rust version, cc'd (slp@ )
Quoted 5 lines
Anyone has other thoughts about how to reduce fragmentation in implementations?
Thanks,
Amir.
Dave
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 99 lines · Amir Goldstein
Quoted 67 lines · Miklos Szeredi
Quoted 22 lines · Vivek Goyal
Quoted 6 lines · Miklos Szeredi
tools/virtiofsd/passthrough_ll.c +2
-0@@ -901,6 +906,18 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, inode =lo_find (lo, &e->attr, mnt_id);if (inode) { +char buf1[PATH_MAX +1 ], buf2[PATH_MAX +1 ], procname[64 ]; +ssize_t siz1, siz2;tools/virtiofsd/passthrough_ll.c static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name, · line 920
Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10
-0+ +sprintf (procname,"%i" , inode->fd); + siz1 =readlinkat (lo->proc_self_fd, procname, buf1,sizeof (buf1)); +sprintf (procname,"%i" , newfd); + siz2 =readlinkat (lo->proc_self_fd, procname, buf2,sizeof (buf2)); + +/* disable close on unlink if alias is detected */ +if (siz1 != siz2 ||memcmp (buf1, buf2, siz1)) +g_atomic_int_inc (&inode->opencount); +Hi Miklos,
Quoted 5 lines
So if I have a hard links to a file in a separate directories, then this path can hit. (Say
dir1/file.txtanddir2/file-link.txt). IIUC, we will disable automaticinode->fdclosing on these hardlinked files. That means this solution will not solve problem at hand for hard linked files. Am I missing something.You are correct, this is a limited solution. The one above is a corner case, and probably not worth worrying about too much. However it can be fixed by having separate
O_PATHfd's open for each alias (or at least for each alias that resides in a distinct directory).NB: doing a synchronous FORGET on unlink does not solve this case either, since the inode will not receive a FORGET until the last alias is dropped.
Quoted 14 lines
Quoted 14 lines
tools/virtiofsd/passthrough_ll.c +1
-0close (newfd); }else { inode =calloc (1 ,sizeof (struct lo_inode ));@@ -917,6 +934,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,*/ g_atomic_int_set (&inode->refcount,2 ); +g_atomic_int_set (&inode->opencount,0 ); inode->nlookup =1 ; inode->fd = newfd; inode->key.ino = e->attr.st_ino;@@ -1295,6 +1313,10 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) res =unlinkat (lo_fd (req, parent), name,0 );Quoted 13 lines
tools/virtiofsd/passthrough_ll.c static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) · line 1319
Quoted 5 lines
tools/virtiofsd/passthrough_ll.c +4
-0fuse_reply_err (req, res ==-1 ? errno :0 ); +if (!g_atomic_int_get (&inode->opencount)) { +close (inode->fd); + inode->fd =-1 ; + }Can this be racy w.r.t
lo_lookup(). IOW, saydir1/file.txtis being unlinked and we closedinode->fd. And before we could executeunref_inode_lolocked(), another parallel lookup ofdir2/file-link.txthappens if it getslo->muxtexlock first, it can still find this inode and bump up reference count. And that meanslo_unlink()will not free inode (but closeinode->fd) and now we will use an inode with closedO_PATHfd which lead to other failures later.Quoted 2 lines
Yes, that's a bug. Also needs serialization against all access to
inode->fd.Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the
FUSE_PASSTHROUGHthreads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
David,
Quoted 8 lines
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.
I'm wondering if fuse-backend-rs is a proper project to work on, and maybe virtiofsd-rs could be switched to use it as well in the future.
Thanks,
Eryu
Quoted 51 lines
Quoted 28 lines
Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the
FUSE_PASSTHROUGHthreads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
David,
Quoted 8 lines
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.
Quoted 7 lines
I'm wondering if fuse-backend-rs is a proper project to work on, and maybe virtiofsd-rs could be switched to use it as well in the future.
Thanks,
Eryu
Hi Eryu!
This looks very interesting.
Can you guys say a few words about the maturity of this project. Does it have any CI? any beta/production workloads that use it? I would be happy to contribute the open_by_handle_at() changes if I know they will get properly tested.
As demonstrated in this demo fs [1], with xfs/ext4 as underlying filesystem, full NFS support can be implemented by implementing lookup in filesystem by inode only, before fuse adds support to LOOKUP_HANDLE in the protocol.
Thanks,
Amir.
Quoted 72 lines · Amir Goldstein
Quoted 52 lines
Quoted 29 lines
Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the
FUSE_PASSTHROUGHthreads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
David,
Quoted 8 lines
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.Quoted 7 lines
I'm wondering if fuse-backend-rs is a proper project to work on, and maybe virtiofsd-rs could be switched to use it as well in the future.
Thanks,
EryuHi Eryu!
This looks very interesting.
Can you guys say a few words about the maturity of this project. Does it have any CI? any beta/production workloads that use it? I would be happy to contribute theopen_by_handle_at()changes if I know they will get properly tested.
Quoted 6 lines
As demonstrated in this demo fs [1], with xfs/ext4 as underlying filesystem, full NFS support can be implemented by implementing lookup in filesystem by inode only, before fuse adds support to
LOOKUP_HANDLEin the protocol.Thanks,
Amir.
Hi Amir,
This project is derived from crosvm and Cloud Hypervisor, and we have
extended it to support some Container centric usage mode. About quality, I think it may be some sort of “product”, but the project itself still lacks of a robust CI system.
The proposal you mentioned about open_by_handle_at() is very attractive:)
We have encountered some cases running out of file descriptors, we have also encountered issues to enabling virtio-fs over nfs. Absolutely the open_by_handle_at() proposal helps here:)
And the next big step of the project is to enable io_uring and rust async io:)
Thanks!
Quoted 2 lines
Quoted 57 lines · Amir Goldstein
Quoted 57 lines
Quoted 28 lines
Miklos,
I would like to point out that this discussion is relevant to any low level fuse filesystem, but especially those that are proxying a real filesystem.
I have raised this question before in the
FUSE_PASSTHROUGHthreads. There is a lot of code duplication among various low-level fuse projects and as we get to NFS export support and complex issues like this one, is it getting unlikely that all projects will handle this correctly.Do you think there is room for some more generic code in libfuse and if so how? Following an example implementation (assuming it gets fixed) and hand picking fixes to projects cannot scale for long.
The challenge is that most of the generic code would be in lookup and maintaining the internal inode cache, but each filesystem may need different representations of the Inode object.
I was thinking of something along the lines of an OO library that implements the generic lookup/inode cache for a base FuseInode class that implementers can inherit from.
This doesn't need to be in the libfuse project at all. Seeing the virtiofsd already has a Rust implementation that is BSD licensed, maybe that can be used as a starting point?
David,
Quoted 8 lines
How hard do you think it would be to re-factor virtiofsd-rs to an implementation that inherits from a base passthroughfsd-rs?
BTW, is virtiofsd-rs the offical virtiofsd or an experimental one? Which tree does Miklos' patch apply to?
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.I'm wondering if fuse-backend-rs is a proper project to work on, and maybe virtiofsd-rs could be switched to use it as well in the future.
Thanks,
Eryu
Quoted 8 lines
Quoted 1 line
Hi Eryu!
This looks very interesting.
Can you guys say a few words about the maturity of this project. Does it have any CI? any beta/production workloads that use it?
We do have secure container workloads running in production env (limited scale for testing though) and using fuse-backend-rs to passthrough fs operations.
Quoted 2 lines
I would be happy to contribute the
open_by_handle_at()changes if I know they will get properly tested.
Yes, as Liu Jiang said, we have similar issues when using viriofs, and are happy to test the open_by_handle code!
Thanks,
Eryu
Quoted 9 lines
As demonstrated in this demo fs [1], with xfs/ext4 as underlying filesystem, full NFS support can be implemented by implementing lookup in filesystem by inode only, before fuse adds support to
LOOKUP_HANDLEin the protocol.Thanks,
Amir.
On Fri, Jan 08, 2021 at 12:12:52PM +0800, Eryu Guan wrote:
[..]
Quoted 6 lines
Quoted 2 lines
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
Quoted 11 lines
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.
So at a high level, is this equivalent of libfuse written in rust?
Vivek
Quoted 8 lines
I'm wondering if fuse-backend-rs is a proper project to work on, and maybe virtiofsd-rs could be switched to use it as well in the future.
Thanks,
Eryu
Quoted 21 lines · Vivek Goyal
On Fri, Jan 08, 2021 at 12:12:52PM +0800, Eryu Guan wrote:
[..]
Quoted 6 lines
Quoted 2 lines
Anyone has other thoughts about how to reduce fragmentation in implementations?
There's an fuse-backend-rs[1] project hosted on cloud-hypervisor, it is a library to communicate with the Linux FUSE clients, which includes:
Quoted 11 lines
- ABI layer, which defines all data structures shared between linux Fuse framework and Fuse daemons.
- API layer, defines the interfaces for Fuse daemons to implement a userspace file system.
- Transport layer, which supports both the Linux Fuse device and virtio-fs protocol.
- VFS/pseudo_fs, an abstraction layer to support multiple file systems by a single virtio-fs device.
- A sample passthrough file system implementation, which passes through files from daemons to clients.
Quoted 1 line
So at a high level, is this equivalent of libfuse written in rust?
Yes, they are similar in concept. Though fuse-backend-rs only provides interfaces like the libfuse lowlevel APIs for filesystem implementations. The fuse session handling part is still outside [1] but we are planning to move it into fuse-backend-rs as well.
Cheers,
Tao
[1] https://github.com/dragonflyoss/image-service/blob/master/utils/src/fuse.rs
Signature · 1 line
Into something rich and strange.
Quoted 69 lines · Amir Goldstein
Quoted 68 lines · Miklos Szeredi
Quoted 65 lines · Amir Goldstein
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open
O_PATHfd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.If you store a FileHandle (
name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2)is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.
Quoted 8 lines
Quoted 6 lines
You are perfectly right that using file handles would solve a number of issues, one being too many open file descriptors.
The issue with
open_by_handle_at(2)is that it needsCAP_DAC_READ_SEARCHin the initial user namespace. That currently makes it impossible to use in containers and such.Is that a problem for virtiofsd? does it also run inside a container??
Yes, there have been cases where virtiofsd is running inside container. For the same reason stefan introduced patches to not setup all the namespace by the daemon. It will be setup by the container manager. And container manager wants to give minimum privilige to virtiofsd container (same capabilities as any other standard container) by default.
Vivek
Quoted 32 lines
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement
open_child_by_handle_at(int parent_fd, ...):
-readdir(parend_fd)
- search a match ford_ino
-name_to_handle_at()and verify match to stored filehandleThis is essentially what
open_by_handle_at(2)does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part. And "connectable" file handles are used by nfsd to enforce "subtree_check" to make sure that file wasn't moved outside obtainable path after initial lookup.Quoted 3 lines
Not sure if there has been proposals for making
open_by_handle_at(2)usable in user namespaces.I don't remember seeing any.
Quoted 2 lines
The difficulty is in verifying that an
open file would have been obtainable by path lookup by the given user.I think it can be allowed for user with
CAP_DAC_READ_SEARCHin userns forFS_USERNS_MOUNTmounted in that userns.Thanks,
Amir.
Quoted 69 lines · * Amir Goldstein ([email protected]) wrote:
Quoted 68 lines · Miklos Szeredi
Quoted 65 lines · Amir Goldstein
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 37 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Question:
a) In the FUSE protocol, are requests assumed to complete in order;
i.e. unlink, forget, rmdir is it required that 'forget' completes
before the rmdir is processed?
(In virtiofs we've been processing requests, in parallel, and
have sent forgets down a separate queue to keep them out of the way).Quoted 2 lines
b) 'forget' doesn't send a reply - so the kernel can't wait for the
client to have finished it; do we need a synchronous forget here?Even if we introduce a synchronous forget, will that really fix the issue. For example, this could also happen if file has been unlinked but it is still open and directory is being removed.
fd = open(foo/bar.txt)
unlinkfoo/bar.txt
rmdir foo
close(fd).In this case, final forget should go after fd has been closed. Its not a forget race.
I wrote a test case for this and it works on regular file systems.
https://github.com/rhvgoyal/misc/blob/master/virtiofs-tests/rmdir.c
Quoted 9 lines
Quoted 4 lines
I suspect it will fail on nfs because I am assuming that temporary file will be there till final close(fd) happens. If that's the case this is a NFS specific issue because its behavior is different from other file systems.
That's true; but that's NFS just being NFS; in our case we're keeping an fd open even though the guest has been smart enough not to; so we're causing the NFS oddity when it wouldn't normally happen.
Are you sure that you really need this oddity?
My sense from looking virtiofsd is that the open
O_PATHfd in InodeData for non-directories are an overkill and even the need for open fd for all directories is questionable.If you store a FileHandle (
name_to_handle_at(2)) instead of an open fd for non-directories, you won't be keeping a reference on the underlying inode so no unlink issue.
open_by_handle_at(2)is very cheap for non-directory when underlying inode is cached and as cheap as it can get even when inode is not in cache, so no performance penalty is expected.
Quoted 8 lines
Quoted 6 lines
You are perfectly right that using file handles would solve a number of issues, one being too many open file descriptors.
The issue with
open_by_handle_at(2)is that it needsCAP_DAC_READ_SEARCHin the initial user namespace. That currently makes it impossible to use in containers and such.Is that a problem for virtiofsd? does it also run inside a container??
Yes it does; certainly for Kata it runs inside a container, and I think it does in some other setups.
Quoted 20 lines
Please note that NFS doesn't do "silly rename" for directories, so mitigation is mostly needed for non-dir.
An alternative method if daemon is not capable, is to store parent dirfd in addition to filehandle and implement
open_child_by_handle_at(int parent_fd, ...):
-readdir(parend_fd)
- search a match ford_ino
-name_to_handle_at()and verify match to stored filehandleThis is essentially what
open_by_handle_at(2)does under the covers with a "connectable" non-dir filehandle after having resolved the parent file handle part. And "connectable" file handles are used by nfsd to enforce "subtree_check" to make sure that file wasn't moved outside obtainable path after initial lookup.Quoted 3 lines
Not sure if there has been proposals for making
open_by_handle_at(2)usable in user namespaces.
Quoted 7 lines
I don't remember seeing any.
Quoted 2 lines
The difficulty is in verifying that an
open file would have been obtainable by path lookup by the given user.I think it can be allowed for user with
CAP_DAC_READ_SEARCHin userns forFS_USERNS_MOUNTmounted in that userns.
It worries me a lot that open_by_handle_at is just too powerful; unless you're giving the user/container it's own fileystem, the guest kernel can basically open any file.
Dave
Quoted 3 lines
Thanks,
Amir.
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 9 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Are you really talking about FORGET and file descriptors here? I always assumed that the kernel will only drop dentries (aka emit FORGET) when all fds are closed and never saw otherwise in practice.
Do you mean RELEASE rather than FORGET, or dentry rather than file descriptor?
Best,
-Nikolaus
Signature · 3 lines
GPG Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F
»Time flies like an arrow, fruit flies like a Banana.«Quoted 12 lines · * Nikolaus Rath ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert
Hi,
On virtio-fs we're hitting a problem with NFS, where
unlinking a file in a directory and then rmdir'ing that directory fails complaining about the directory not being empty.The problem here is that if a file has an open fd, NFS doesn't actually delete the file on unlink, it just renames it to a hidden file (e.g. .nfs*******). That open file is there because the 'FORGET' hasn't completed yet by the time the rmdir is issued.
Quoted 5 lines
Are you really talking about FORGET and file descriptors here? I always assumed that the kernel will only drop dentries (aka emit FORGET) when all fds are closed and never saw otherwise in practice.
Do you mean RELEASE rather than FORGET, or dentry rather than file descriptor?
<reads code again> I still think I mean FORGET, and deamons fd.
In the (passthrough_ll) daemon we have two fd's; if I've got this right then RELEASE closes fi->fh (i.e. a normal fd) where as FORGET will close inode->fd which is the O_PATH fd.
If the user was to do:
1 touch a/x
2 rm a/x
3 rmdir a/x
They would close(2) the fd at the end of the touch; so I think that would cause us to see the RELEASE - but we don't see the FORGET yet. Only after the 'rm' when a/x shouldn't exist any more; then I think we can get a FORGET and close our inode->fd
Dave
Quoted 8 lines
Best,
-NikolausSignature · 3 lines
GPG Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F »Time flies like an arrow, fruit flies like a Banana.«
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK