nfsd: clean up share access with accessor functions
Subsystems (2)
cover nfsd: clean up share access with accessor functions
While looking over the share/deny mode stuff in knfsd, I ended up doing some cleanup to make the code a bit easier to understand. In the future this may help us by encapsulating the share/deny code to some degree.
This set should introduce no behavioral changes, it's just cleanup for now, but is probably reasonable for 3.5.
Jeff Layton (4):
nfsd: consolidate set_access and set_deny
nfsd: make test_share a bool return
nfsd: wrap accesses to st_access_bmap
nfsd: wrap all accesses to st_deny_bmap
fs/nfsd/nfs4state.c | 149 ++++++++++++++++++++++++++++++++------------------- 1 files changed, 93 insertions(+), 56 deletions(-)
Signature · 1 line
1.7.7.6
PATCH 1/4 nfsd: consolidate set_access and set_deny
These functions are identical. Also, rename them to bmap_to_share_mode to better reflect what they do, and have them just return the result instead of passing in a pointer to the storage location.
--- fs/nfsd/nfs4state.c | 24 +++++++----------------- 1 files changed, 7 insertions(+), 17 deletions(-)
fs/nfsd/nfs4state.c +7 -17
@@ -447,34 +447,24 @@ static struct list_head close_lru;* * which we should reject. */ -static void -set_access (unsigned int *access,unsigned long bmap) { +static unsigned int +bmap_to_share_mode (unsigned long bmap) {int i; +unsigned int access =0 ; - *access =0 ;for (i =1 ; i <4 ; i++) {if (test_bit (i, &bmap)) - *access |= i; - } -} - -static void -set_deny (unsigned int *deny,unsigned long bmap) { -int i; - - *deny =0 ; -for (i =0 ; i <4 ; i++) { -if (test_bit (i, &bmap)) - *deny |= i ; + access |= i; } +return access; }static int test_share (struct nfs4_ol_stateid *stp,struct nfsd4_open *open) {unsigned int access, deny; -set_access (&access, stp->st_access_bmap); -set_deny (&deny, stp->st_deny_bmap); + access =bmap_to_share_mode (stp->st_access_bmap); + deny =bmap_to_share_mode (stp->st_deny_bmap);if ((access & open->op_share_deny) || (deny & open->op_share_access))return 0 ;return 1 ;
Signature · 1 line
1.7.7.6
PATCH 2/4 nfsd: make test_share a bool return
In reply to Jeff LaytonAll of the callers treat the return that way already.
--- fs/nfsd/nfs4state.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +3 -3
@@ -459,15 +459,15 @@ bmap_to_share_mode(unsigned long bmap) {return access; } -static int +static bool test_share (struct nfs4_ol_stateid *stp,struct nfsd4_open *open) {unsigned int access, deny; access =bmap_to_share_mode (stp->st_access_bmap); deny =bmap_to_share_mode (stp->st_deny_bmap);if ((access & open->op_share_deny) || (deny & open->op_share_access)) -return 0 ; -return 1 ; +return false; +return true; }static int nfs4_access_to_omode (u32 access)
Signature · 1 line
1.7.7.6
PATCH 3/4 nfsd: wrap accesses to st_access_bmap
In reply to Jeff LaytonCurrently, we do this for the most part with "bare" bitops, but eventually we'll need to expand the share mode code to handle access and deny modes on other nodes.
In order to facilitate that code in the future, move to some generic accessor functions. For now, these are mostly static inlines, but eventually we'll want to move these to "real" functions that are able to handle multi-node configurations or have a way to "swap in" new operations to be done in lieu of or in conjunction with these atomic bitops.
--- fs/nfsd/nfs4state.c | 82 +++++++++++++++++++++++++++++++++----------------- 1 files changed, 54 insertions(+), 28 deletions(-)
fs/nfsd/nfs4state.c +54 -28
@@ -470,6 +470,27 @@ test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {return true; } +/* set share access for a given stateid */ +static inline void +set_access (u32 access,struct nfs4_ol_stateid *stp) +{ +__set_bit (access, &stp->st_access_bmap); +} + +/* clear share access for a given stateid */ +static inline void +clear_access (u32 access,struct nfs4_ol_stateid *stp) +{ +__clear_bit (access, &stp->st_access_bmap); +} + +/* test whether a given stateid has access */ +static inline bool +test_access (u32 access,struct nfs4_ol_stateid *stp) +{ +return test_bit (access, &stp->st_access_bmap); +} +static int nfs4_access_to_omode (u32 access) {switch (access &NFS4_SHARE_ACCESS_BOTH ) {
@@ -483,6 +504,20 @@ static int nfs4_access_to_omode(u32 access)BUG (); } +/* release all access and file references for a given stateid */ +static void +release_all_access (struct nfs4_ol_stateid *stp) +{ +int i; + +for (i =1 ; i <4 ; i++) { +if (test_access (i, stp)) +nfs4_file_put_access (stp->st_file, +nfs4_access_to_omode (i)); +clear_access (i, stp); + } +} +static void unhash_generic_stateid (struct nfs4_ol_stateid *stp) {list_del (&stp->st_perfile);
@@ -491,16 +526,7 @@ static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)static void close_generic_stateid (struct nfs4_ol_stateid *stp) { -int i; - -if (stp->st_access_bmap) { -for (i =1 ; i <4 ; i++) { -if (test_bit (i, &stp->st_access_bmap)) -nfs4_file_put_access (stp->st_file, -nfs4_access_to_omode (i)); -__clear_bit (i, &stp->st_access_bmap); - } - } +release_all_access (stp);put_nfs4_file (stp->st_file); stp->st_file =NULL ; }
@@ -2444,7 +2470,7 @@ static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, stp->st_file = fp; stp->st_access_bmap =0 ; stp->st_deny_bmap =0 ; -__set_bit (open->op_share_access, &stp->st_access_bmap); +set_access (open->op_share_access, stp);__set_bit (open->op_share_deny, &stp->st_deny_bmap); stp->st_openstp =NULL ; }
@@ -2781,7 +2807,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cbool new_access;__be32 status; - new_access = !test_bit (op_share_access, &stp->st_access_bmap); + new_access = !test_access (op_share_access, stp);if (new_access) { status =nfs4_get_vfs_file (rqstp, fp, cur_fh, open);if (status)
@@ -2796,7 +2822,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *creturn status; }/* remember the open */ -__set_bit (op_share_access, &stp->st_access_bmap); +set_access (op_share_access, stp);__set_bit (open->op_share_deny, &stp->st_deny_bmap);return nfs_ok;
@@ -3266,18 +3292,18 @@ STALE_STATEID(stateid_t *stateid) }static inline int -access_permit_read (unsigned long access_bmap) +access_permit_read (struct nfs4_ol_stateid *stp) { -return test_bit (NFS4_SHARE_ACCESS_READ , &access_bmap) || -test_bit (NFS4_SHARE_ACCESS_BOTH , &access_bmap) || -test_bit (NFS4_SHARE_ACCESS_WRITE , &access_bmap); +return test_access (NFS4_SHARE_ACCESS_READ , stp) || +test_access (NFS4_SHARE_ACCESS_BOTH , stp) || +test_access (NFS4_SHARE_ACCESS_WRITE , stp); }static inline int -access_permit_write (unsigned long access_bmap) +access_permit_write (struct nfs4_ol_stateid *stp) { -return test_bit (NFS4_SHARE_ACCESS_WRITE , &access_bmap) || -test_bit (NFS4_SHARE_ACCESS_BOTH , &access_bmap); +return test_access (NFS4_SHARE_ACCESS_WRITE , stp) || +test_access (NFS4_SHARE_ACCESS_BOTH , stp); } static
@@ -3288,9 +3314,9 @@ __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)/* For lock stateid's, we test the parent open, not the lock: */ if (stp->st_openstp) stp = stp->st_openstp; -if ((flags &WR_STATE ) && (!access_permit_write (stp->st_access_bmap))) +if ((flags &WR_STATE ) && !access_permit_write (stp)) goto out; -if ((flags &RD_STATE ) && (!access_permit_read (stp->st_access_bmap))) +if ((flags &RD_STATE ) && !access_permit_read (stp)) goto out; status = nfs_ok; out:
@@ -3639,10 +3665,10 @@ out:static inline void nfs4_stateid_downgrade_bit (struct nfs4_ol_stateid *stp,u32 access) { -if (!test_bit (access, &stp->st_access_bmap)) +if (!test_access (access, stp))return ;nfs4_file_put_access (stp->st_file,nfs4_access_to_omode (access)); -__clear_bit (access, &stp->st_access_bmap); +clear_access (access, stp); }static inline void nfs4_stateid_downgrade (struct nfs4_ol_stateid *stp,u32 to_access)
@@ -3696,8 +3722,8 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp,if (status) goto out; status = nfserr_inval; -if (!test_bit (od->od_share_access, &stp->st_access_bmap)) { -dprintk ("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n" , +if (!test_access (od->od_share_access, stp)) { +dprintk ("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n" , stp->st_access_bmap, od->od_share_access); goto out; }
@@ -3998,10 +4024,10 @@ static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)struct nfs4_file *fp = lock_stp->st_file;int oflag =nfs4_access_to_omode (access); -if (test_bit (access, &lock_stp->st_access_bmap)) +if (test_access (access, lock_stp))return ;nfs4_file_get_access (fp, oflag); -__set_bit (access, &lock_stp->st_access_bmap); +set_access (access, lock_stp); }__be32 lookup_or_create_lock_state (struct nfsd4_compound_state *cstate,struct nfs4_ol_stateid *ost,struct nfsd4_lock *lock,struct nfs4_ol_stateid **lst,bool *new)
Signature · 1 line
1.7.7.6
PATCH 4/4 nfsd: wrap all accesses to st_deny_bmap
In reply to Jeff LaytonHandle the st_deny_bmap in a similar fashion to the st_access_bmap. Add accessor functions and use those instead of bare bitops.
--- fs/nfsd/nfs4state.c | 37 +++++++++++++++++++++++++++++-------- 1 files changed, 29 insertions(+), 8 deletions(-)
fs/nfsd/nfs4state.c +29 -8
@@ -491,6 +491,27 @@ test_access(u32 access, struct nfs4_ol_stateid *stp)return test_bit (access, &stp->st_access_bmap); } +/* set share deny for a given stateid */ +static inline void +set_deny (u32 access,struct nfs4_ol_stateid *stp) +{ +__set_bit (access, &stp->st_deny_bmap); +} + +/* clear share deny for a given stateid */ +static inline void +clear_deny (u32 access,struct nfs4_ol_stateid *stp) +{ +__clear_bit (access, &stp->st_deny_bmap); +} + +/* test whether a given stateid is denying specific access */ +static inline bool +test_deny (u32 access,struct nfs4_ol_stateid *stp) +{ +return test_bit (access, &stp->st_deny_bmap); +} +static int nfs4_access_to_omode (u32 access) {switch (access &NFS4_SHARE_ACCESS_BOTH ) {
@@ -2471,7 +2492,7 @@ static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, stp->st_access_bmap =0 ; stp->st_deny_bmap =0 ;set_access (open->op_share_access, stp); -__set_bit (open->op_share_deny, &stp->st_deny_bmap); +set_deny (open->op_share_deny, stp); stp->st_openstp =NULL ; }
@@ -2550,8 +2571,8 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) ret = nfserr_locked;/* Search for conflicting share reservations */ list_for_each_entry (stp, &fp->fi_stateids, st_perfile) { -if (test_bit (deny_type, &stp->st_deny_bmap) || -test_bit (NFS4_SHARE_DENY_BOTH , &stp->st_deny_bmap)) +if (test_deny (deny_type, stp) || +test_deny (NFS4_SHARE_DENY_BOTH , stp)) goto out; } ret = nfs_ok;
@@ -2823,7 +2844,7 @@ nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *c }/* remember the open */ set_access (op_share_access, stp); -__set_bit (open->op_share_deny, &stp->st_deny_bmap); +set_deny (open->op_share_deny, stp);return nfs_ok; }
@@ -3690,12 +3711,12 @@ static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_ac }static void -reset_union_bmap_deny (unsigned long deny,unsigned long *bmap) +reset_union_bmap_deny (unsigned long deny,struct nfs4_ol_stateid *stp) {int i;for (i =0 ; i <4 ; i++) {if ((i & deny) != i) -__clear_bit (i, bmap); +clear_deny (i, stp); } }
@@ -3727,14 +3748,14 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp, stp->st_access_bmap, od->od_share_access); goto out; } -if (!test_bit (od->od_share_deny, &stp->st_deny_bmap)) { +if (!test_deny (od->od_share_deny, stp)) {dprintk ("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n" , stp->st_deny_bmap, od->od_share_deny); goto out; }nfs4_stateid_downgrade (stp, od->od_share_access); -reset_union_bmap_deny (od->od_share_deny, &stp->st_deny_bmap); +reset_union_bmap_deny (od->od_share_deny, stp);update_stateid (&stp->st_stid.sc_stateid);memcpy (&od->od_stateid, &stp->st_stid.sc_stateid,sizeof (stateid_t));
Signature · 1 line
1.7.7.6
Quoted 6 lines · Jeff Layton
While looking over the share/deny mode stuff in knfsd, I ended up doing some cleanup to make the code a bit easier to understand. In the future this may help us by encapsulating the share/deny code to some degree.
This set should introduce no behavioral changes, it's just cleanup for now, but is probably reasonable for 3.5.
Thanks, those look fine, applying.--b.
Quoted 13 lines
Jeff Layton (4):
nfsd: consolidateset_accessandset_deny
nfsd: maketest_sharea bool return
nfsd: wrap accesses tost_access_bmap
nfsd: wrap all accesses tost_deny_bmapfs/nfsd/nfs4state.c | 149 ++++++++++++++++++++++++++++++++------------------- 1 files changed, 93 insertions(+), 56 deletions(-)Signature · 1 line
1.7.7.6