OCFS2: anti stale inode for nfs (V3)
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
For nfs exporting, ocfs2_get_dentry() returns the dentry for fh. ocfs2_get_dentry() may read from disk(when inode not in memory) without any cross cluster lock. this leads to load a stale inode.
this patch fixes above problem.
solution is that in case of inode is not in memory, we get the cluster lock(PR) of alloc inode where the inode in question is allocated from(this causes node on which deletion is done sync the alloc inode) before reading out the inode its self. then we check the bitmap in the group(the inode in question allcated from) to see if the bit is clear. if it's clear then it's stale. if the bit is set, we then check generation as the existing code.
actually we have to read out the inode in question from disk(not cache) first to know its alloc slot(tells alloc inode) and allot bit(tells alloc group). and if its not stale(by above logic) we read it out using ocfs2_iget(). the second read should from cache.
and also we have to add a per superblock nfs_sync_lock to cover the lock for alloc inode and that for inode in question. this is because ocfs2_get_dentry() and ocfs2_delete_inode() lock on them in reverse order. nfs_sync_lock is locked in EX mode in ocfs2_get_dentry() and in PR mode in ocfs2_delete_inode(). so that mutliple ocfs2_delete_inode() can run concurrently in normal case.
this patch is based on 1.4 git.
--
dlmglue.c | 45 +++++++++++++++++++++++ dlmglue.h | 2 + export.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++------ inode.c | 23 +++++++++++ inode.h | 1 ocfs2.h | 1 ocfs2_lockid.h | 4 ++ suballoc.c | 75 ++++++++++++++++++++++++++++++++++++++ suballoc.h | 7 +++ 9 files changed, 258 insertions(+), 11 deletions(-)
ocfs2/export.c +101 -10
@@ -38,6 +38,8 @@#include "inode.h" #include "buffer_head_io.h" +#include "sysfile.h" +#include "suballoc.h" struct ocfs2_inode_handle {
@@ -48,35 +50,124 @@ struct ocfs2_inode_handlestatic struct dentry *ocfs2_get_dentry (struct super_block *sb,void *vobjp) {struct ocfs2_inode_handle *handle = vobjp; -struct inode *inode; +struct inode *inode, *inode_alloc_inode; +struct ocfs2_super *osb =OCFS2_SB (sb); +struct buffer_head *alloc_bh =NULL ; +u64 blkno = handle->ih_blkno; +u16 suballoc_bit, suballoc_slot;struct dentry *result; +int status, set;mlog_entry ("(0x%p, 0x%p)\n" , sb, handle); -if (handle->ih_blkno ==0 ) { -mlog_errno (-ESTALE ); -return ERR_PTR (-ESTALE ); +if (blkno ==0 ) { +mlog (0 ,"nfs wants inode with blkno: 0\n" ); + result =ERR_PTR (-ESTALE ); + goto bail; + } + + inode =ocfs2_ilookup (sb, blkno); +/* found in-memory inode, goes to check generation */ +if (inode) + goto check_gen; + +/* takes nfs_sync_lock in EX mode */ + status =ocfs2_nfs_sync_lock (osb,1 ); +if (status <0 ) { +mlog (ML_ERROR ,"getting nfs sync lock(EX) failed %d\n" , status); + goto check_err; } - inode =ocfs2_iget (OCFS2_SB (sb), handle->ih_blkno,0 ,0 ); + status =ocfs2_get_suballoc_slot_bit (osb, blkno, &suballoc_slot, + &suballoc_bit); +if (status <0 ) { +if (status == -EINVAL ) { +/* meta block never be re-allocated as data block. +* nfsd gives us wrong blkno */ + status = -EEXIST ; + }else { +mlog (ML_ERROR ,"get alloc slot and bit failed %d\n" , + status); + } + goto unlock_nfs_sync; + } + inode_alloc_inode = +ocfs2_get_system_file_inode (osb,INODE_ALLOC_SYSTEM_INODE , + suballoc_slot); +if (!inode_alloc_inode) { + status = -EEXIST ; +mlog (ML_ERROR ,"unable to get alloc inode in slot %u\n" , + (u32 )suballoc_slot); + goto unlock_nfs_sync; + } + +mutex_lock (&inode_alloc_inode->i_mutex); + status =ocfs2_inode_lock (inode_alloc_inode, &alloc_bh,0 ); +if (status <0 ) { +mlog (ML_ERROR ,"lock on alloc inode on slot %u failed %d\n" , + (u32 )suballoc_slot, status); + goto unlock_mutex; + } + status =ocfs2_test_suballoc_bit (osb, inode_alloc_inode, alloc_bh, + blkno, suballoc_bit, &set); +if (status <0 ) { +mlog (ML_ERROR ,"test suballoc bit failed %d\n" , status); + goto inode_unlock; + } +/* allocate bit is clear, inode is a stale inode */ +if (!set) { + status = -ESTALE ; + goto inode_unlock; + } -if (IS_ERR (inode)) -return (void *)inode; + inode =ocfs2_iget (osb, blkno,0 ,0 ); +inode_unlock: +ocfs2_inode_unlock (inode_alloc_inode,0 ); + +unlock_mutex: +mutex_unlock (&inode_alloc_inode->i_mutex); +iput (inode_alloc_inode); +brelse (alloc_bh); + +unlock_nfs_sync: +ocfs2_nfs_sync_unlock (osb,1 ); + +check_err: +if (status <0 ) { +if (status == -ESTALE ) { +mlog (0 ,"stale inode ino: %llu generation: %u\n" , + blkno, handle->ih_generation); + } + result =ERR_PTR (status); + goto bail; + } + +if (IS_ERR (inode)) { +mlog_errno ((int )inode); + result = (void *)inode; + goto bail; + } + +check_gen:if (handle->ih_generation != inode->i_generation) {iput (inode); -return ERR_PTR (-ESTALE ); +mlog (0 ,"stale inode ino: %llu generation: %u\n" , blkno, + handle->ih_generation); + result =ERR_PTR (-ESTALE ); + goto bail; } result =d_alloc_anon (inode);if (!result) {iput (inode); -mlog_errno (-ENOMEM ); -return ERR_PTR (-ENOMEM ); + result =ERR_PTR (-ENOMEM ); + goto bail; } result->d_op = &ocfs2_dentry_ops; +bail:mlog_exit_ptr (result);return result; }
ocfs2/inode.c +22 -1
@@ -111,6 +111,18 @@ void ocfs2_get_inode_flags(struct ocfs2_ oi->ip_attr |=OCFS2_DIRSYNC_FL ; } +struct inode *ocfs2_ilookup (struct super_block *sb,u64 blkno) +{ +struct ocfs2_find_inode_args args; + + args.fi_blkno = blkno; + args.fi_flags =0 ; + args.fi_ino =ino_from_blkno (sb, blkno); + args.fi_sysfile_type =0 ; + +return ilookup5 (sb, blkno, ocfs2_find_actor, &args); +} +struct inode *ocfs2_iget (struct ocfs2_super *osb,u64 blkno,unsigned flags,int sysfile_type) {
@@ -933,6 +945,13 @@ void ocfs2_delete_inode(struct inode *in goto bail; } +/* Lock down the nfs_sync lock in PR mode */ + status =ocfs2_nfs_sync_lock (OCFS2_SB (inode->i_sb),0 ); +if (status <0 ) { +mlog (ML_ERROR ,"getting nfs sync lock(PR) failed %d\n" , status); +ocfs2_cleanup_delete_inode (inode,0 ); + goto bail_unblock; + }/* Lock down the inode. This gives us an up to date view of * it's metadata (for verification), and allows us to * serialize delete_inode on multiple nodes.
@@ -946,7 +965,7 @@ void ocfs2_delete_inode(struct inode *inif (status != -ENOENT )mlog_errno (status);ocfs2_cleanup_delete_inode (inode,0 ); - goto bail_unblock; + goto bail_unlock_nfs_sync; }/* Query the cluster. This will be the final decision made
@@ -989,6 +1008,8 @@ void ocfs2_delete_inode(struct inode *in bail_unlock_inode:ocfs2_inode_unlock (inode,1 );brelse (di_bh); +bail_unlock_nfs_sync: +ocfs2_nfs_sync_unlock (OCFS2_SB (inode->i_sb),0 ); bail_unblock: status =sigprocmask (SIG_SETMASK , &oldset,NULL );if (status <0 )
ocfs2/suballoc.c +75 -0
78 lines not on this page · raw message
ocfs2/suballoc.h +7 -0
@@ -157,4 +157,11 @@ u64 ocfs2_which_cluster_group(struct inoint ocfs2_check_group_descriptor (struct super_block *sb,struct ocfs2_dinode *di,struct ocfs2_group_desc *gd); + +int ocfs2_get_suballoc_slot_bit (struct ocfs2_super *osb,u64 blkno, +u16 *suballoc_slot,u16 *suballoc_bit); + +int ocfs2_test_suballoc_bit (struct ocfs2_super *osb,struct inode *suballoc, +struct buffer_head *alloc_bh,u64 blkno,u16 bit, +int *res);#endif /* _CHAINALLOC_H_ */
ocfs2/dlmglue.h +2 -0
@@ -99,6 +99,8 @@ void ocfs2_super_unlock(struct ocfs2_sup int ex);int ocfs2_rename_lock (struct ocfs2_super *osb);void ocfs2_rename_unlock (struct ocfs2_super *osb); +int ocfs2_nfs_sync_lock (struct ocfs2_super *osb,int ex); +void ocfs2_nfs_sync_unlock (struct ocfs2_super *osb,int ex);int ocfs2_dentry_lock (struct dentry *dentry,int ex);void ocfs2_dentry_unlock (struct dentry *dentry,int ex);int ocfs2_file_lock (struct file *file,int ex,int trylock);
ocfs2/inode.h +1 -0
@@ -126,6 +126,7 @@ void ocfs2_drop_inode(struct inode *inod/* Flags for ocfs2_iget() */ #define OCFS2_FI_FLAG_SYSFILE 0x1#define OCFS2_FI_FLAG_ORPHAN_RECOVERY 0x2 +struct inode *ocfs2_ilookup (struct super_block *sb,u64 feoff);struct inode *ocfs2_iget (struct ocfs2_super *osb,u64 feoff,unsigned flags,int sysfile_type);int ocfs2_inode_init_private (struct inode *inode);
ocfs2/ocfs2_lockid.h +4 -0
@@ -46,6 +46,7 @@ enum ocfs2_lock_type {OCFS2_LOCK_TYPE_DENTRY ,OCFS2_LOCK_TYPE_OPEN ,OCFS2_LOCK_TYPE_FLOCK , +OCFS2_LOCK_TYPE_NFS_SYNC , OCFS2_NUM_LOCK_TYPES };
@@ -77,6 +78,9 @@ static inline char ocfs2_lock_type_char(case OCFS2_LOCK_TYPE_FLOCK : c ='F' ;break ; +case OCFS2_LOCK_TYPE_NFS_SYNC : + c ='Y' ; +break ;default : c ='\0' ; }
ocfs2/ocfs2.h +1 -0
@@ -288,6 +288,7 @@ struct ocfs2_superstruct dlm_ctxt *dlm;struct ocfs2_lock_res osb_super_lockres;struct ocfs2_lock_res osb_rename_lockres; +struct ocfs2_lock_res osb_nfs_sync_lockres;struct dlm_eviction_cb osb_eviction_cb;struct ocfs2_dlm_debug *osb_dlm_debug;struct dlm_protocol_version osb_locking_proto;
ocfs2/dlmglue.c +45 -0
81 lines not on this page · raw message
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
Quoted 5 lines · wengang wang
For nfs exporting,
ocfs2_get_dentry()returns the dentry for fh.ocfs2_get_dentry()may read from disk(when inode not in memory) without any cross cluster lock. this leads to load a stale inode.this patch fixes above problem.
This patch is almost there. Excellent!
Quoted 1 line
this patch is based on 1.4 git.
Going forward, fixes really need to be against mainline. Let's
finish out this patch against 1.4 and then you can port it to mainline. But for the future, we fix against mainline and backport.
Quoted 31 lines
ocfs2/export.c +31
-0+ status =ocfs2_get_suballoc_slot_bit (osb, blkno, &suballoc_slot, + &suballoc_bit); +if (status <0 ) { +if (status == -EINVAL ) { +/* meta block never be re-allocated as data block. +* nfsd gives us wrong blkno */ + status = -EEXIST ; + }else { +mlog (ML_ERROR ,"get alloc slot and bit failed %d\n" , + status); + } + goto unlock_nfs_sync; + } + inode_alloc_inode = +ocfs2_get_system_file_inode (osb,INODE_ALLOC_SYSTEM_INODE , + suballoc_slot); +if (!inode_alloc_inode) { + status = -EEXIST ; +mlog (ML_ERROR ,"unable to get alloc inode in slot %u\n" , + (u32 )suballoc_slot); + goto unlock_nfs_sync; + } + +mutex_lock (&inode_alloc_inode->i_mutex); + status =ocfs2_inode_lock (inode_alloc_inode, &alloc_bh,0 ); +if (status <0 ) { +mlog (ML_ERROR ,"lock on alloc inode on slot %u failed %d\n" , + (u32 )suballoc_slot, status); + goto unlock_mutex; + } + status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
ocfs2/export.c struct ocfs2_inode_handle · line 121
Quoted 10 lines
ocfs2/export.c +10
-0+ blkno, suballoc_bit, &set); +if (status <0 ) { +mlog (ML_ERROR ,"test suballoc bit failed %d\n" , status); + goto inode_unlock; + } +/* allocate bit is clear, inode is a stale inode */ +if (!set) { + status = -ESTALE ; + goto inode_unlock; + }
You can drop the suballocator lock here. Taking the lock has
made sure that other nodes flushed their journals. You have just validated that the bit is set, and other nodes cannot clear the bit until they get the nfs_sync lock, which you already hold. So it is safe to call ocfs2_inode_unlock(inode_alloc_inode, 0) and mutex_unlock(&inode_alloc_inode->i_mutex) before calling ocfs2_iget().
This has two benefits. Number 1, we don't take the suballoc
lock and the inode lock (in iget()) at the same time. The fewer locks we take at the same time, the better. Number 2, this means the entire suballocator lookup code above can be made into a subfunction. This improves the readability of the code.
Quoted 15 lines
ocfs2/suballoc.c +15
-0+/* reads(hit disk) the inode specified by blkno to get suballoc_slot +* and suballoc_bit +* */ +int ocfs2_get_suballoc_slot_bit (struct ocfs2_super *osb,u64 blkno, +u16 *suballoc_slot,u16 *suballoc_bit) +{ +int status; +struct buffer_head *inode_bh =NULL ; +struct ocfs2_dinode *inode_fe; + +mlog_entry ("blkno: %llu\n" , blkno); + +/* dirty read disk */ + status =ocfs2_read_block (osb, blkno, &inode_bh,0 ,NULL ); +if (status <0 )
ocfs2/suballoc.c static inline void ocfs2_debug_suballoc_ · line 1913
Quoted 10 lines
ocfs2/suballoc.c +10
-0+ goto bail; + + inode_fe = (struct ocfs2_dinode *) inode_bh->b_data; +if (!OCFS2_IS_VALID_DINODE (inode_fe)) { + status = -EINVAL ; + goto bail; + } + +if (suballoc_slot) + *suballoc_slot =le16_to_cpu (inode_fe->i_suballoc_slot);
Probably want to validate that suballoc_slot is within the range
of valid slot numbers. Just in case.
Otherwise, everything looks good. The nfs_sync_lock is good.
It will need to be added to debugfs.ocfs2's lock displays.
Joel
Signature · 10 lines
"Baby, even the losers Get luck sometimes. Even the losers Keep a little bit of pride." Joel Becker Principal Software Developer Oracle E-mail: joel.becker at oracle.com Phone: (650) 506-8127
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
Quoted 6 lines
Quoted 1 line
this patch is based on 1.4 git.
Going forward, fixes really need to be against mainline. Let's
finish out this patch against 1.4 and then you can port it to mainline. But for the future, we fix against mainline and backport.
Ok, I will follow that from next posting.
Quoted 13 lines
You can drop the suballocator lock here. Taking the lock has
made sure that other nodes flushed their journals. You have just validated that the bit is set, and other nodes cannot clear the bit until they get thenfs_synclock, which you already hold. So it is safe to callocfs2_inode_unlock(inode_alloc_inode, 0)andmutex_unlock(&inode_alloc_inode->i_mutex)before callingocfs2_iget().
This has two benefits. Number 1, we don't take the suballoc
lock and the inode lock (iniget()) at the same time. The fewer locks we take at the same time, the better. Number 2, this means the entire suballocator lookup code above can be made into a subfunction. This improves the readability of the code.
good point! will revise the code.
Quoted 5 lines
ocfs2/suballoc.c static inline void ocfs2_debug_suballoc_ · line 1913
Quoted 2 lines
ocfs2/suballoc.c +2
-0+if (suballoc_slot) + *suballoc_slot =le16_to_cpu (inode_fe->i_suballoc_slot);Probably want to validate that
suballoc_slotis within the range
of valid slot numbers. Just in case.
in what case could the suballoc_slot be out of range?
Quoted 2 lines
Otherwise, everything looks good. The
nfs_sync_lockis good.
It will need to be added to debugfs.ocfs2's lock displays.
next post will be against mainline. and the patch for debugfs.ocfs2 will be available later to ocfs2-tools-devel@
thanks,
wengang.
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
Quoted 5 lines · Wengang Wang
Quoted 4 lines
ocfs2/suballoc.c static inline void ocfs2_debug_suballoc_ · line 1913
Quoted 2 lines
ocfs2/suballoc.c +2
-0+if (suballoc_slot) + *suballoc_slot =le16_to_cpu (inode_fe->i_suballoc_slot);Probably want to validate that
suballoc_slotis within the range
of valid slot numbers. Just in case.in what case could the
suballoc_slotbe out of range?
Filesystem corruption or a bad NFS handle of some sort. It's
easy to check that i_suballoc_slot is either OCFS2_INVALID_SLOT or within osb->max_slots.
Joel
Signature · 9 lines
Dort wo man B?cher verbrennt, verbrennt man am Ende auch Mensch. (Wherever they burn books, they will also end up burning people.) - Heinrich Heine Joel Becker Principal Software Developer Oracle E-mail: joel.becker at oracle.com Phone: (650) 506-8127
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
In reply to Joel BeckerQuoted 1 line
It will need to be added to debugfs.ocfs2's lock displays.
what's this in detail?
the display for "debugfs.ocfs2 -d <lockres>"? such as the rename lock
[root at ocfs2-2 ~]# debugfs.ocfs2 -d R000000000000000000000000000000
Lockres: R000000000000000000000000000000 Type: Rename Block: 0 Generation: 0x00000000
?
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
Quoted 9 lines · Wengang Wang
Quoted 1 line
It will need to be added to debugfs.ocfs2's lock displays.
what's this in detail?
the display for "debugfs.ocfs2 -d <lockres>"? such as the rename lock[root at ocfs2-2 ~]# debugfs.ocfs2 -d R000000000000000000000000000000
Lockres: R000000000000000000000000000000 Type: Rename Block: 0 Generation: 0x00000000
Basically, libocfs2/lockid.c needs to be updated to understand
the new lock type. debugfs should then be able to encode/decode the lockres nicely. You should be able to copy the behavior of the rename lock just like you did in the kernel.
Joel
Signature · 9 lines
"I always thought the hardest questions were those I could not answer. Now I know they are the ones I can never ask." - Charlie Watkins Joel Becker Principal Software Developer Oracle E-mail: joel.becker at oracle.com Phone: (650) 506-8127
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
In reply to Wengang WangFinish the kernel bits first. Once that is checked in, the following files in the tools will need to be updated.
=> libocfs2/lockid.c
=> include/ocfs2-kernel/ocfs2_lockid.h (copy from kernel)
Quoted 18 lines · Wengang Wang
Quoted 2 lines
It will need to be added to debugfs.ocfs2's lock displays.
what's this in detail?
the display for "debugfs.ocfs2 -d <lockres>"? such as the rename lock[root at ocfs2-2 ~]# debugfs.ocfs2 -d R000000000000000000000000000000
Lockres: R000000000000000000000000000000 Type: Rename Block: 0 Generation: 0x00000000?
_______________________________________________
Ocfs2-devel mailing list
Ocfs2-devel at oss.oracle.com
http://oss.oracle.com/mailman/listinfo/ocfs2-devel
PATCH 1/1 OCFS2: anti stale inode for nfs (V3)
Hi Sunil,
There is still something left for ocfs2 tools to support nfs_sync lock.
Quoted 2 lines · Sunil Mushran
Finish the kernel bits first. Once that is checked in, the following files in the tools will need to be updated.
here you meant checked in 1.4git or mainline?
thanks,
wengang.
Quoted 16 lines
=>
libocfs2/lockid.c
=>include/ocfs2-kernel/ocfs2_lockid.h(copy from kernel)Quoted 12 lines · Wengang Wang
Quoted 2 lines
It will need to be added to debugfs.ocfs2's lock displays.
what's this in detail?
the display for "debugfs.ocfs2 -d <lockres>"? such as the rename lock[root at ocfs2-2 ~]# debugfs.ocfs2 -d R000000000000000000000000000000
Lockres: R000000000000000000000000000000 Type: Rename Block: 0 Generation: 0x00000000?