Skip to content

Commit 819ccbf

Browse files
committed
ceph: fix leaked inode ref due to snap metadata writeback race
We create a ceph_cap_snap if there is dirty cap metadata (for writeback to mds) OR dirty pages (for writeback to osd). It is thus possible that the metadata has been written back to the MDS but the OSD data has not when the cap_snap is created. This results in a cap_snap with dirty(caps) == 0. The problem is that cap writeback to the MDS isn't necessary, and a FLUSHSNAP cap op gets no ack from the MDS. This leaves the cap_snap attached to the inode along with its inode reference. Fix the problem by dropping the cap_snap if it becomes 'complete' (all pages written out) and dirty(caps) == 0 in ceph_put_wrbuffer_cap_refs(). Also, BUG() in __ceph_flush_snaps() if we encounter a cap_snap with dirty(caps) == 0. Signed-off-by: Sage Weil <sage@newdream.net>
1 parent 6298a33 commit 819ccbf

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

‎fs/ceph/caps.c‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,12 @@ void __ceph_flush_snaps(struct ceph_inode_info *ci,
12041204
if (capsnap->dirty_pages || capsnap->writing)
12051205
continue;
12061206

1207+
/*
1208+
* if cap writeback already occurred, we should have dropped
1209+
* the capsnap in ceph_put_wrbuffer_cap_refs.
1210+
*/
1211+
BUG_ON(capsnap->dirty == 0);
1212+
12071213
/* pick mds, take s_mutex */
12081214
mds = __ceph_get_cap_mds(ci, &mseq);
12091215
if (session && session->s_mds != mds) {
@@ -2117,8 +2123,8 @@ void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
21172123
}
21182124
spin_unlock(&inode->i_lock);
21192125

2120-
dout("put_cap_refs %p had %s %s\n", inode, ceph_cap_string(had),
2121-
last ? "last" : "");
2126+
dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2127+
last ? " last" : "", put ? " put" : "");
21222128

21232129
if (last && !flushsnaps)
21242130
ceph_check_caps(ci, 0, NULL);
@@ -2142,7 +2148,8 @@ void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
21422148
{
21432149
struct inode *inode = &ci->vfs_inode;
21442150
int last = 0;
2145-
int last_snap = 0;
2151+
int complete_capsnap = 0;
2152+
int drop_capsnap = 0;
21462153
int found = 0;
21472154
struct ceph_cap_snap *capsnap = NULL;
21482155

@@ -2165,30 +2172,45 @@ void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
21652172
list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
21662173
if (capsnap->context == snapc) {
21672174
found = 1;
2168-
capsnap->dirty_pages -= nr;
2169-
last_snap = !capsnap->dirty_pages;
21702175
break;
21712176
}
21722177
}
21732178
BUG_ON(!found);
2179+
capsnap->dirty_pages -= nr;
2180+
if (capsnap->dirty_pages == 0) {
2181+
complete_capsnap = 1;
2182+
if (capsnap->dirty == 0)
2183+
/* cap writeback completed before we created
2184+
* the cap_snap; no FLUSHSNAP is needed */
2185+
drop_capsnap = 1;
2186+
}
21742187
dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2175-
" snap %lld %d/%d -> %d/%d %s%s\n",
2188+
" snap %lld %d/%d -> %d/%d %s%s%s\n",
21762189
inode, capsnap, capsnap->context->seq,
21772190
ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
21782191
ci->i_wrbuffer_ref, capsnap->dirty_pages,
21792192
last ? " (wrbuffer last)" : "",
2180-
last_snap ? " (capsnap last)" : "");
2193+
complete_capsnap ? " (complete capsnap)" : "",
2194+
drop_capsnap ? " (drop capsnap)" : "");
2195+
if (drop_capsnap) {
2196+
ceph_put_snap_context(capsnap->context);
2197+
list_del(&capsnap->ci_item);
2198+
list_del(&capsnap->flushing_item);
2199+
ceph_put_cap_snap(capsnap);
2200+
}
21812201
}
21822202

21832203
spin_unlock(&inode->i_lock);
21842204

21852205
if (last) {
21862206
ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
21872207
iput(inode);
2188-
} else if (last_snap) {
2208+
} else if (complete_capsnap) {
21892209
ceph_flush_snaps(ci);
21902210
wake_up(&ci->i_cap_wq);
21912211
}
2212+
if (drop_capsnap)
2213+
iput(inode);
21922214
}
21932215

21942216
/*
@@ -2464,8 +2486,8 @@ static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
24642486
break;
24652487
}
24662488
WARN_ON(capsnap->dirty_pages || capsnap->writing);
2467-
dout(" removing cap_snap %p follows %lld\n",
2468-
capsnap, follows);
2489+
dout(" removing %p cap_snap %p follows %lld\n",
2490+
inode, capsnap, follows);
24692491
ceph_put_snap_context(capsnap->context);
24702492
list_del(&capsnap->ci_item);
24712493
list_del(&capsnap->flushing_item);

‎fs/ceph/snap.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,17 @@ int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
521521
capsnap->ctime = inode->i_ctime;
522522
capsnap->time_warp_seq = ci->i_time_warp_seq;
523523
if (capsnap->dirty_pages) {
524-
dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu "
524+
dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
525525
"still has %d dirty pages\n", inode, capsnap,
526526
capsnap->context, capsnap->context->seq,
527-
capsnap->size, capsnap->dirty_pages);
527+
ceph_cap_string(capsnap->dirty), capsnap->size,
528+
capsnap->dirty_pages);
528529
return 0;
529530
}
530-
dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu clean\n",
531+
dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
531532
inode, capsnap, capsnap->context,
532-
capsnap->context->seq, capsnap->size);
533+
capsnap->context->seq, ceph_cap_string(capsnap->dirty),
534+
capsnap->size);
533535

534536
spin_lock(&mdsc->snap_flush_lock);
535537
list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);

0 commit comments

Comments
 (0)