NFSv4.1: Ask for no delegation on OPEN if using O_DIRECT
Subsystems (2)
PATCH 1/2 NFSv4.1: Ask for no delegation on OPEN if using O_DIRECT
If we're using NFSv4.1, then we have the ability to let the server know whether or not we believe that returning a delegation as part of our OPEN request would be useful.
The feature needs to be used with care, since the client sending the request doesn't necessarily know how other clients are using that file, and how they may be affected by the delegation.
For this reason, our initial use of the feature will be to let the server know when the client believes that handing out a delegation would not be useful.
The first application for this function is when opening the file using O_DIRECT.
--- fs/nfs/nfs4proc.c | 30 +++++++++++++++++++ fs/nfs/nfs4xdr.c | 79 +++++++++++++++++++++++++++++++++---------------- include/linux/nfs_xdr.h | 2 ++ 3 files changed, 85 insertions(+), 26 deletions(-)
fs/nfs/nfs4proc.c +30 -0
@@ -940,6 +940,31 @@ static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,return true; } +static u32 +nfs4_map_atomic_open_share (struct nfs_server *server, +fmode_t fmode,int openflags) +{ +u32 res =0 ; + +switch (fmode & (FMODE_READ |FMODE_WRITE )) { +case FMODE_READ : + res =NFS4_SHARE_ACCESS_READ ; +break ; +case FMODE_WRITE : + res =NFS4_SHARE_ACCESS_WRITE ; +break ; +case FMODE_READ |FMODE_WRITE : + res =NFS4_SHARE_ACCESS_BOTH ; + } +if (!(server->caps &NFS_CAP_ATOMIC_OPEN_V1 )) + goto out; +/* Want no delegation if we're using O_DIRECT */ +if (openflags &O_DIRECT ) + res |=NFS4_SHARE_WANT_NO_DELEG ; +out: +return res; +} +static enum open_claim_type4 nfs4_map_atomic_open_claim (struct nfs_server *server,enum open_claim_type4 claim)
@@ -1002,6 +1027,8 @@ static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,atomic_inc (&sp->so_count); p->o_arg.open_flags = flags; p->o_arg.fmode = fmode & (FMODE_READ |FMODE_WRITE ); + p->o_arg.share_access =nfs4_map_atomic_open_share (server, + fmode, flags);/* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS * will return permission denied for all bits until close */ if (!(flags &O_EXCL )) {
@@ -2695,6 +2722,9 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data) goto out_wait; } } + calldata->arg.share_access = +nfs4_map_atomic_open_share (NFS_SERVER (inode), + calldata->arg.fmode,0 );nfs_fattr_init (calldata->res.fattr); calldata->timestamp = jiffies;
fs/nfs/nfs4xdr.c +53 -26
117 lines not on this page · raw message
include/linux/nfs_xdr.h +2 -0
@@ -326,6 +326,7 @@ struct nfs_openargs {struct nfs_seqid * seqid;int open_flags;fmode_t fmode; +u32 share_access;u32 access;__u64 clientid;struct stateowner_id id;
@@ -393,6 +394,7 @@ struct nfs_closeargs {nfs4_stateid stateid;struct nfs_seqid * seqid;fmode_t fmode; +u32 share_access;const u32 * bitmask; };
Signature · 1 line
2.1.0
PATCH 2/2 NFSv4.1: Ask for no delegation on OPEN if already holding one
If we already hold a delegation, there should be no reason for the server to issue it to us again. Unfortunately, there appear to be servers out there that engage in this practice. While it is often harmless to do so, there is one case where this creates a problem and that is when the client is in the process of returning that delegation.
This patch uses the NFSv4.1 NFS4_SHARE_WANT_NO_DELEG flag to inform the server not to return a delegation in these cases.
--- fs/nfs/nfs4proc.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
fs/nfs/nfs4proc.c +22 -3
@@ -942,8 +942,10 @@ static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,static u32 nfs4_map_atomic_open_share (struct nfs_server *server, +struct inode *inode,fmode_t fmode,int openflags) { +struct nfs_delegation *delegation;u32 res =0 ;switch (fmode & (FMODE_READ |FMODE_WRITE )) {
@@ -959,8 +961,25 @@ nfs4_map_atomic_open_share(struct nfs_server *server,if (!(server->caps &NFS_CAP_ATOMIC_OPEN_V1 )) goto out;/* Want no delegation if we're using O_DIRECT */ -if (openflags &O_DIRECT ) +if (openflags &O_DIRECT ) { res |=NFS4_SHARE_WANT_NO_DELEG ; + goto out; + } +if (inode ==NULL ) + goto out; +rcu_read_lock (); + delegation =rcu_dereference (NFS_I (inode)->delegation); +/* +* If we have a delegation, either ask for an upgrade or ask for +* no delegation +*/ +if (delegation) { +if ((fmode &FMODE_WRITE ) && !(delegation->type &FMODE_WRITE )) + res |=NFS4_SHARE_WANT_WRITE_DELEG ; +else + res |=NFS4_SHARE_WANT_NO_DELEG ; + } +rcu_read_unlock (); out:return res; }
@@ -1028,7 +1047,7 @@ static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, p->o_arg.open_flags = flags; p->o_arg.fmode = fmode & (FMODE_READ |FMODE_WRITE ); p->o_arg.share_access =nfs4_map_atomic_open_share (server, - fmode, flags); + dentry->d_inode, fmode, flags);/* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS * will return permission denied for all bits until close */ if (!(flags &O_EXCL )) {
@@ -2724,7 +2743,7 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data) } calldata->arg.share_access =nfs4_map_atomic_open_share (NFS_SERVER (inode), - calldata->arg.fmode,0 ); +NULL , calldata->arg.fmode,0 );nfs_fattr_init (calldata->res.fattr); calldata->timestamp = jiffies;
Signature · 1 line
2.1.0
Re: [PATCH 2/2] NFSv4.1: Ask for no delegation on OPEN if already holding one
DQo+IE9uIEZlYiAzLCAyMDE1LCBhdCA0OjU5IFBNLCBUcm9uZCBNeWtsZWJ1c3QgPHRyb25kLm15 a2xlYnVzdEBwcmltYXJ5ZGF0YS5jb20+IHdyb3RlOg0KPiANCj4gSWYgd2UgYWxyZWFkeSBob2xk IGEgZGVsZWdhdGlvbiwgdGhlcmUgc2hvdWxkIGJlIG5vIHJlYXNvbiBmb3IgdGhlDQo+IHNlcnZl ciB0byBpc3N1ZSBpdCB0byB1cyBhZ2Fpbi4gVW5mb3J0dW5hdGVseSwgdGhlcmUgYXBwZWFyIHRv IGJlDQo+IHNlcnZlcnMgb3V0IHRoZXJlIHRoYXQgZW5nYWdlIGluIHRoaXMgcHJhY3RpY2UuIFdo aWxlIGl0IGlzIG9mdGVuDQo+IGhhcm1sZXNzIHRvI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 X21hcF9hdG9taWNfb3Blbl9zaGFyZShORlNfU0VSVkVSKGlub2RlKSwNCj4gLQkJCQljYWxsZGF0 YS0+YXJnLmZtb2RlLCAwKTsNCj4gKwkJCQlOVUxMLCBjYWxsZGF0YS0+YXJnLmZtb2RlLCAwKTsN Cj4gDQo+IAluZnNfZmF0dHJfaW5pdChjYWxsZGF0YS0+cmVzLmZhdHRyKTsNCj4gCWNhbGxkYXRh LT50aW1lc3RhbXAgPSBqaWZmaWVzOw0KPiAtLSANCj4gMi4xLjANCj4gDQoNCg0KSGkgVHJvbmQs DQoNCkRvIHlvdSBzZWUgdGhpcyBvZiBoZWxwaW5nIHdpdGggdGhlIHJhY2U/IFdlIHdvbuKAmXQg ZmluZCBhIGRlbGVnYXRpb24gb24gdGhlIHJhY2luZyBvcGVuIGFzIGl0IGhhcyBiZWVuIGRldGFj aGVkIGZyb20gdGhlIGlub2RlLiBGb3IgdGhlIG90aGVyIHBhdGNoLCBhcmVu4oCZdCB3ZSBhbHJl YWR5IGNoZWNraW5nIGlmIHdlIGNhbiBkbyBhIGxvY2FsIG9wZW4gYnkgY2hlY2tpbmcgZm9yIHRo ZSBkZWxlZ2F0aW9uIChjYW5fb3Blbl9kZWxlZ2F0ZWQoKSk/DQoNClRoYW5rcy4NCg0K
Re: [PATCH 2/2] NFSv4.1: Ask for no delegation on OPEN if already holding one
Quoted 84 lines · Olga
Quoted 78 lines · Trond Myklebust
If we already hold a delegation, there should be no reason for the server to issue it to us again. Unfortunately, there appear to be servers out there that engage in this practice. While it is often harmless to do so, there is one case where this creates a problem and that is when the client is in the process of returning that delegation.
This patch uses the NFSv4.1NFS4_SHARE_WANT_NO_DELEGflag to inform the server not to return a delegation in these cases.Signed-off-by Trond Myklebust <[email protected]>---
fs/nfs/nfs4proc.c| 25 ++++++++++++++++++++++---1 file changed, 22 insertions(+), 3 deletions(-)fs/nfs/nfs4proc.c +0
-0@@ -942,8 +942,10 @@ static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,static u32
nfs4_map_atomic_open_share(structnfs_server*server,
+ struct inode *inode,
fmode_tfmode, int openflags)
{fs/nfs/nfs4proc.c +19
-1+struct nfs_delegation *delegation;u32 res =0 ;switch (fmode & (FMODE_READ |FMODE_WRITE )) {@@ -959,8 +961,25 @@ nfs4_map_atomic_open_share(struct nfs_server *server,if (!(server->caps &NFS_CAP_ATOMIC_OPEN_V1 )) goto out;/* Want no delegation if we're using O_DIRECT */ -if (openflags &O_DIRECT ) +if (openflags &O_DIRECT ) { res |=NFS4_SHARE_WANT_NO_DELEG ; + goto out; + } +if (inode ==NULL ) + goto out; +rcu_read_lock (); + delegation =rcu_dereference (NFS_I (inode)->delegation); +/* +* If we have a delegation, either ask for an upgrade or ask for +* no delegation +*/ +if (delegation) { +if ((fmode &FMODE_WRITE ) && !(delegation->type &FMODE_WRITE )) + res |=NFS4_SHARE_WANT_WRITE_DELEG ; +else + res |=NFS4_SHARE_WANT_NO_DELEG ; + } +rcu_read_unlock ();out:
return res; }fs/nfs/nfs4proc.c +2
-3@@ -1028,7 +1047,7 @@ static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, p->o_arg.open_flags = flags; p->o_arg.fmode = fmode & (FMODE_READ |FMODE_WRITE ); p->o_arg.share_access =nfs4_map_atomic_open_share (server, - fmode, flags); + dentry->d_inode, fmode, flags);/* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS * will return permission denied for all bits until close */ if (!(flags &O_EXCL )) {@@ -2724,7 +2743,7 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data) } calldata->arg.share_access =nfs4_map_atomic_open_share (NFS_SERVER (inode), - calldata->arg.fmode,0 ); +NULL , calldata->arg.fmode,0 );nfs_fattr_init (calldata->res.fattr); calldata->timestamp = jiffies; --Quoted 2 lines
2.1.0
Hi Trond,
Quoted 2 lines
Do you see this of helping with the race? We won’t find a delegation on the racing open as it has been detached from the inode. For the other patch, aren’t we already checking if we can do a local open by checking for the delegation (
can_open_delegated())?
Argh. You're 100% right in that it won't completely close the race. However it narrows the race to the delegreturn itself, whereas currently we also have a race while in the process of reclaiming opens
+ locks.
As for patch 1/2, I believe that O_DIRECT opens are still a valid case.
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, PrimaryData [email protected]
Re: [PATCH 2/2] NFSv4.1: Ask for no delegation on OPEN if already holding one
In reply to Trond MyklebustQuoted 8 lines · Trond Myklebust
If we already hold a delegation, there should be no reason for the server to issue it to us again. Unfortunately, there appear to be servers out there that engage in this practice. While it is often harmless to do so, there is one case where this creates a problem and that is when the client is in the process of returning that delegation.
This patch uses the NFSv4.1NFS4_SHARE_WANT_NO_DELEGflag to inform the server not to return a delegation in these cases.
Shouldn't this be patch 1 as it's the actual bug fix that might need backporting?
Re: [PATCH 2/2] NFSv4.1: Ask for no delegation on OPEN if already holding one
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Trond Myklebust
If we already hold a delegation, there should be no reason for the server to issue it to us again. Unfortunately, there appear to be servers out there that engage in this practice. While it is often harmless to do so, there is one case where this creates a problem and that is when the client is in the process of returning that delegation.
This patch uses the NFSv4.1NFS4_SHARE_WANT_NO_DELEGflag to inform the server not to return a delegation in these cases.
Quoted 2 lines
Shouldn't this be patch 1 as it's the actual bug fix that might need backporting?
I'm dropping it instead. It doesn't completely close the race left open by the lack of clarity in the protocol, and so it is counterproductive; it will leave sloppy server vendors thinking the problem is solved when it isn't.
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, PrimaryData [email protected]
Quoted 11 lines · Trond Myklebust
If we're using NFSv4.1, then we have the ability to let the server know whether or not we believe that returning a delegation as part of our OPEN request would be useful.
The feature needs to be used with care, since the client sending the request doesn't necessarily know how other clients are using that file, and how they may be affected by the delegation.
For this reason, our initial use of the feature will be to let the server know when the client believes that handing out a delegation would not be useful.
The first application for this function is when opening the file usingO_DIRECT.
I can't see why O_DIRECT openers would not want to use a delegation, can you explain your rationale?
Quoted 13 lines · Christoph Hellwig
Quoted 11 lines · Trond Myklebust
If we're using NFSv4.1, then we have the ability to let the server know whether or not we believe that returning a delegation as part of our OPEN request would be useful.
The feature needs to be used with care, since the client sending the request doesn't necessarily know how other clients are using that file, and how they may be affected by the delegation.
For this reason, our initial use of the feature will be to let the server know when the client believes that handing out a delegation would not be useful.
The first application for this function is when opening the file usingO_DIRECT.
Quoted 3 lines
I can't see why
O_DIRECTopeners would not want to use a delegation, can you explain your rationale?
Delegations are all about allowing the NFS client to cache data aggressively, and notifying when it is no longer safe to do so. That is clearly not of interest to an application using O_DIRECT, since it is by definition managing the data cache (if there is one) instead of the NFS client. We don't share delegation state with userspace and even if we did, there are no existing applications out there that are capable (or even interested) of taking advantage of it.
You can argue that the client could still use the delegation to cache metadata and open/lock state, but most of the users of O_DIRECT of which I'm aware tend to be data intensive, and not very metadata/state intensive. So why burden both the server and the with that extra state management?
Cheers
Trond
Quoted 8 lines · Trond Myklebust
Delegations are all about allowing the NFS client to cache data aggressively, and notifying when it is no longer safe to do so. That is clearly not of interest to an application using
O_DIRECT, since it is by definition managing the data cache (if there is one) instead of the NFS client. We don't share delegation state with userspace and even if we did, there are no existing applications out there that are capable (or even interested) of taking advantage of it.
Quoted 5 lines
You can argue that the client could still use the delegation to cache metadata and open/lock state, but most of the users of
O_DIRECTof which I'm aware tend to be data intensive, and not very metadata/state intensive. So why burden both the server and the with that extra state management?
How does the delegation hurt us in this case? That needs to go into the patch description, and into a comment near the code.