Skip to content

Commit 26be880

Browse files
Alex ElderSage Weil
authored andcommitted
libceph: change how "safe" callback is used
An osd request currently has two callbacks. They inform the initiator of the request when we've received confirmation for the target osd that a request was received, and when the osd indicates all changes described by the request are durable. The only time the second callback is used is in the ceph file system for a synchronous write. There's a race that makes some handling of this case unsafe. This patch addresses this problem. The error handling for this callback is also kind of gross, and this patch changes that as well. In ceph_sync_write(), if a safe callback is requested we want to add the request on the ceph inode's unsafe items list. Because items on this list must have their tid set (by ceph_osd_start_request()), the request added *after* the call to that function returns. The problem with this is that there's a race between starting the request and adding it to the unsafe items list; the request may already be complete before ceph_sync_write() even begins to put it on the list. To address this, we change the way the "safe" callback is used. Rather than just calling it when the request is "safe", we use it to notify the initiator the bounds (start and end) of the period during which the request is *unsafe*. So the initiator gets notified just before the request gets sent to the osd (when it is "unsafe"), and again when it's known the results are durable (it's no longer unsafe). The first call will get made in __send_request(), just before the request message gets sent to the messenger for the first time. That function is only called by __send_queued(), which is always called with the osd client's request mutex held. We then have this callback function insert the request on the ceph inode's unsafe list when we're told the request is unsafe. This will avoid the race because this call will be made under protection of the osd client's request mutex. It also nicely groups the setup and cleanup of the state associated with managing unsafe requests. The name of the "safe" callback field is changed to "unsafe" to better reflect its new purpose. It has a Boolean "unsafe" parameter to indicate whether the request is becoming unsafe or is now safe. Because the "msg" parameter wasn't used, we drop that. This resolves the original problem reportedin: http://tracker.ceph.com/issues/4706 Reported-by: Yan, Zheng <zheng.z.yan@intel.com> Signed-off-by: Alex Elder <elder@inktank.com> Reviewed-by: Yan, Zheng <zheng.z.yan@intel.com> Reviewed-by: Sage Weil <sage@inktank.com>
1 parent 7d7d51c commit 26be880

3 files changed

Lines changed: 40 additions & 28 deletions

File tree

‎fs/ceph/file.c‎

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -446,19 +446,35 @@ static ssize_t ceph_sync_read(struct file *file, char __user *data,
446446
}
447447

448448
/*
449-
* Write commit callback, called if we requested both an ACK and
450-
* ONDISK commit reply from the OSD.
449+
* Write commit request unsafe callback, called to tell us when a
450+
* request is unsafe (that is, in flight--has been handed to the
451+
* messenger to send to its target osd). It is called again when
452+
* we've received a response message indicating the request is
453+
* "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
454+
* is completed early (and unsuccessfully) due to a timeout or
455+
* interrupt.
456+
*
457+
* This is used if we requested both an ACK and ONDISK commit reply
458+
* from the OSD.
451459
*/
452-
static void sync_write_commit(struct ceph_osd_request *req,
453-
struct ceph_msg *msg)
460+
static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
454461
{
455462
struct ceph_inode_info *ci = ceph_inode(req->r_inode);
456463

457-
dout("sync_write_commit %p tid %llu\n", req, req->r_tid);
458-
spin_lock(&ci->i_unsafe_lock);
459-
list_del_init(&req->r_unsafe_item);
460-
spin_unlock(&ci->i_unsafe_lock);
461-
ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
464+
dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
465+
unsafe ? "un" : "");
466+
if (unsafe) {
467+
ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
468+
spin_lock(&ci->i_unsafe_lock);
469+
list_add_tail(&req->r_unsafe_item,
470+
&ci->i_unsafe_writes);
471+
spin_unlock(&ci->i_unsafe_lock);
472+
} else {
473+
spin_lock(&ci->i_unsafe_lock);
474+
list_del_init(&req->r_unsafe_item);
475+
spin_unlock(&ci->i_unsafe_lock);
476+
ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
477+
}
462478
}
463479

464480
/*
@@ -570,7 +586,8 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data,
570586

571587
if ((file->f_flags & O_SYNC) == 0) {
572588
/* get a second commit callback */
573-
req->r_safe_callback = sync_write_commit;
589+
req->r_unsafe_callback = ceph_sync_write_unsafe;
590+
req->r_inode = inode;
574591
own_pages = true;
575592
}
576593
}
@@ -581,21 +598,8 @@ static ssize_t ceph_sync_write(struct file *file, const char __user *data,
581598
ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
582599

583600
ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
584-
if (!ret) {
585-
if (req->r_safe_callback) {
586-
/*
587-
* Add to inode unsafe list only after we
588-
* start_request so that a tid has been assigned.
589-
*/
590-
spin_lock(&ci->i_unsafe_lock);
591-
list_add_tail(&req->r_unsafe_item,
592-
&ci->i_unsafe_writes);
593-
spin_unlock(&ci->i_unsafe_lock);
594-
ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
595-
}
596-
601+
if (!ret)
597602
ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
598-
}
599603

600604
if (file->f_flags & O_DIRECT)
601605
ceph_put_page_vector(pages, num_pages, false);

‎include/linux/ceph/osd_client.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct ceph_authorizer;
2929
*/
3030
typedef void (*ceph_osdc_callback_t)(struct ceph_osd_request *,
3131
struct ceph_msg *);
32+
typedef void (*ceph_osdc_unsafe_callback_t)(struct ceph_osd_request *, bool);
3233

3334
/* a given osd we're communicating with */
3435
struct ceph_osd {
@@ -149,7 +150,8 @@ struct ceph_osd_request {
149150
struct kref r_kref;
150151
bool r_mempool;
151152
struct completion r_completion, r_safe_completion;
152-
ceph_osdc_callback_t r_callback, r_safe_callback;
153+
ceph_osdc_callback_t r_callback;
154+
ceph_osdc_unsafe_callback_t r_unsafe_callback;
153155
struct ceph_eversion r_reassert_version;
154156
struct list_head r_unsafe_item;
155157

‎net/ceph/osd_client.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,14 @@ static void __send_request(struct ceph_osd_client *osdc,
13141314
list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
13151315

13161316
ceph_msg_get(req->r_request); /* send consumes a ref */
1317-
ceph_con_send(&req->r_osd->o_con, req->r_request);
1317+
1318+
/* Mark the request unsafe if this is the first timet's being sent. */
1319+
1320+
if (!req->r_sent && req->r_unsafe_callback)
1321+
req->r_unsafe_callback(req, true);
13181322
req->r_sent = req->r_osd->o_incarnation;
1323+
1324+
ceph_con_send(&req->r_osd->o_con, req->r_request);
13191325
}
13201326

13211327
/*
@@ -1403,8 +1409,8 @@ static void handle_osds_timeout(struct work_struct *work)
14031409

14041410
static void complete_request(struct ceph_osd_request *req)
14051411
{
1406-
if (req->r_safe_callback)
1407-
req->r_safe_callback(req, NULL);
1412+
if (req->r_unsafe_callback)
1413+
req->r_unsafe_callback(req, false);
14081414
complete_all(&req->r_safe_completion); /* fsync waiter */
14091415
}
14101416

0 commit comments

Comments
 (0)