Skip to content

Commit db35405

Browse files
committed
ceph: fix cap flush race reentrancy
In e9964c1 we change cap flushing to do a delicate dance because some inodes on the cap_dirty list could be in a migrating state (got EXPORT but not IMPORT) in which we couldn't actually flush and move from dirty->flushing, breaking the while (!empty) { process first } loop structure. It worked for a single sync thread, but was not reentrant and triggered infinite loops when multiple syncers came along. Instead, move inodes with dirty to a separate cap_dirty_migrating list when in the limbo export-but-no-import state, allowing us to go back to the simple loop structure (which was reentrant). This is cleaner and more robust. Audited the cap_dirty users and this looks fine: list_empty(&ci->i_dirty_item) is still a reliable indicator of whether we have dirty caps (which list we're on is irrelevant) and list_del_init() calls still do the right thing. Signed-off-by: Sage Weil <sage@newdream.net>
1 parent cd634fb commit db35405

3 files changed

Lines changed: 31 additions & 29 deletions

File tree

‎fs/ceph/caps.c‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,6 +2635,7 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
26352635
struct ceph_mds_session *session,
26362636
int *open_target_sessions)
26372637
{
2638+
struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
26382639
struct ceph_inode_info *ci = ceph_inode(inode);
26392640
int mds = session->s_mds;
26402641
unsigned mseq = le32_to_cpu(ex->migrate_seq);
@@ -2671,6 +2672,19 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
26712672
* export targets, so that we get the matching IMPORT
26722673
*/
26732674
*open_target_sessions = 1;
2675+
2676+
/*
2677+
* we can't flush dirty caps that we've seen the
2678+
* EXPORT but no IMPORT for
2679+
*/
2680+
spin_lock(&mdsc->cap_dirty_lock);
2681+
if (!list_empty(&ci->i_dirty_item)) {
2682+
dout(" moving %p to cap_dirty_migrating\n",
2683+
inode);
2684+
list_move(&ci->i_dirty_item,
2685+
&mdsc->cap_dirty_migrating);
2686+
}
2687+
spin_unlock(&mdsc->cap_dirty_lock);
26742688
}
26752689
__ceph_remove_cap(cap);
26762690
}
@@ -2708,6 +2722,13 @@ static void handle_cap_import(struct ceph_mds_client *mdsc,
27082722
ci->i_cap_exporting_issued = 0;
27092723
ci->i_cap_exporting_mseq = 0;
27102724
ci->i_cap_exporting_mds = -1;
2725+
2726+
spin_lock(&mdsc->cap_dirty_lock);
2727+
if (!list_empty(&ci->i_dirty_item)) {
2728+
dout(" moving %p back to cap_dirty\n", inode);
2729+
list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2730+
}
2731+
spin_unlock(&mdsc->cap_dirty_lock);
27112732
} else {
27122733
dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
27132734
inode, ci, mds, mseq);
@@ -2911,38 +2932,16 @@ void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
29112932
*/
29122933
void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
29132934
{
2914-
struct ceph_inode_info *ci, *nci = NULL;
2915-
struct inode *inode, *ninode = NULL;
2916-
struct list_head *p, *n;
2935+
struct ceph_inode_info *ci;
2936+
struct inode *inode;
29172937

29182938
dout("flush_dirty_caps\n");
29192939
spin_lock(&mdsc->cap_dirty_lock);
2920-
list_for_each_safe(p, n, &mdsc->cap_dirty) {
2921-
if (nci) {
2922-
ci = nci;
2923-
inode = ninode;
2924-
ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2925-
dout("flush_dirty_caps inode %p (was next inode)\n",
2926-
inode);
2927-
} else {
2928-
ci = list_entry(p, struct ceph_inode_info,
2929-
i_dirty_item);
2930-
inode = igrab(&ci->vfs_inode);
2931-
BUG_ON(!inode);
2932-
dout("flush_dirty_caps inode %p\n", inode);
2933-
}
2934-
if (n != &mdsc->cap_dirty) {
2935-
nci = list_entry(n, struct ceph_inode_info,
2936-
i_dirty_item);
2937-
ninode = igrab(&nci->vfs_inode);
2938-
BUG_ON(!ninode);
2939-
nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2940-
dout("flush_dirty_caps next inode %p, noflush\n",
2941-
ninode);
2942-
} else {
2943-
nci = NULL;
2944-
ninode = NULL;
2945-
}
2940+
while (!list_empty(&mdsc->cap_dirty)) {
2941+
ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
2942+
i_dirty_item);
2943+
inode = igrab(&ci->vfs_inode);
2944+
dout("flush_dirty_caps %p\n", inode);
29462945
spin_unlock(&mdsc->cap_dirty_lock);
29472946
if (inode) {
29482947
ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
@@ -2952,6 +2951,7 @@ void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
29522951
spin_lock(&mdsc->cap_dirty_lock);
29532952
}
29542953
spin_unlock(&mdsc->cap_dirty_lock);
2954+
dout("flush_dirty_caps done\n");
29552955
}
29562956

29572957
/*

‎fs/ceph/mds_client.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,7 @@ int ceph_mdsc_init(struct ceph_fs_client *fsc)
30043004
spin_lock_init(&mdsc->snap_flush_lock);
30053005
mdsc->cap_flush_seq = 0;
30063006
INIT_LIST_HEAD(&mdsc->cap_dirty);
3007+
INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
30073008
mdsc->num_cap_flushing = 0;
30083009
spin_lock_init(&mdsc->cap_dirty_lock);
30093010
init_waitqueue_head(&mdsc->cap_flushing_wq);

‎fs/ceph/mds_client.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ struct ceph_mds_client {
278278

279279
u64 cap_flush_seq;
280280
struct list_head cap_dirty; /* inodes with dirty caps */
281+
struct list_head cap_dirty_migrating; /* ...that are migration... */
281282
int num_cap_flushing; /* # caps we are flushing */
282283
spinlock_t cap_dirty_lock; /* protects above items */
283284
wait_queue_head_t cap_flushing_wq;

0 commit comments

Comments
 (0)