From patchwork Mon Dec 3 12:50:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709507 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4DB9016B1 for ; Mon, 3 Dec 2018 12:56:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F00A2B415 for ; Mon, 3 Dec 2018 12:56:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 331E42B54C; Mon, 3 Dec 2018 12:56:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0A7E2B415 for ; Mon, 3 Dec 2018 12:56:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726594AbeLCM52 (ORCPT ); Mon, 3 Dec 2018 07:57:28 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22276 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726490AbeLCM52 (ORCPT ); Mon, 3 Dec 2018 07:57:28 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S3; Mon, 03 Dec 2018 20:50:39 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 01/11] libceph: support prefix and suffix in bio_iter Date: Mon, 3 Dec 2018 07:50:25 -0500 Message-Id: <1543841435-13652-2-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4DZr17JF1fKrW8WFW8Crg_yoW7Xw4kpF sIga1kGa4fGan7JFW3tF1xAF4fJr93WrW0grZ8Jay5Gw17Jry5JFyIkr9YgF4xKr4kWF1a yrs8AayUCryrJaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbiOJrUUUUU= X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiGwASelpcgQPvqAAAsw Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we want to add some prefix to the bio data, such as journal header, we don't want to copy it from bio into a new larger memory. Instead, we need a prefix page and suffix page to store the header and footer in bio_iter. Signed-off-by: Dongsheng Yang --- include/linux/ceph/messenger.h | 9 ++++ net/ceph/messenger.c | 96 ++++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/include/linux/ceph/messenger.h b/include/linux/ceph/messenger.h index 800a212..0da6a4b 100644 --- a/include/linux/ceph/messenger.h +++ b/include/linux/ceph/messenger.h @@ -87,6 +87,15 @@ enum ceph_msg_data_type { struct ceph_bio_iter { struct bio *bio; struct bvec_iter iter; + size_t bio_len; + + struct page *prefix_page; + unsigned int prefix_offset; + unsigned int prefix_len; + + struct page *suffix_page; + unsigned int suffix_offset; + unsigned int suffix_len; }; #define __ceph_bio_iter_advance_step(it, n, STEP) do { \ diff --git a/net/ceph/messenger.c b/net/ceph/messenger.c index 57fcc6b..64c70c5 100644 --- a/net/ceph/messenger.c +++ b/net/ceph/messenger.c @@ -828,23 +828,46 @@ static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor, cursor->resid = min_t(size_t, length, data->bio_length); *it = data->bio_pos; - if (cursor->resid < it->iter.bi_size) - it->iter.bi_size = cursor->resid; - BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); - cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); + if (cursor->resid < it->iter.bi_size + it->prefix_len + it->suffix_len) + it->iter.bi_size = cursor->resid - it->prefix_len - it->suffix_len; + + it->bio_len = cursor->resid - it->prefix_len - it->suffix_len; + if (it->suffix_len) { + cursor->last_piece = cursor->resid == it->suffix_len; + } else if (it->bio_len) { + cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); + } else if (it->prefix_len) { + cursor->last_piece = cursor->resid == it->prefix_len; + } else { + BUG(); + } } static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor, size_t *page_offset, size_t *length) { - struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio, - cursor->bio_iter.iter); + struct ceph_bio_iter *it = &cursor->bio_iter; - *page_offset = bv.bv_offset; - *length = bv.bv_len; - return bv.bv_page; + if (it->prefix_len) { + *page_offset = it->prefix_offset; + *length = it->prefix_len; + return it->prefix_page; + } else if (it->bio_len) { + struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio, + cursor->bio_iter.iter); + + *page_offset = bv.bv_offset; + *length = bv.bv_len; + return bv.bv_page; + } else { + BUG_ON(!it->suffix_len); + + *page_offset = it->suffix_offset; + *length = it->suffix_len; + return it->suffix_page; + } } static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor, @@ -852,29 +875,58 @@ static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor, { struct ceph_bio_iter *it = &cursor->bio_iter; + if (!bytes) + return false; + BUG_ON(bytes > cursor->resid); - BUG_ON(bytes > bio_iter_len(it->bio, it->iter)); cursor->resid -= bytes; - bio_advance_iter(it->bio, &it->iter, bytes); + if (it->prefix_len) { + BUG_ON(bytes > it->prefix_len); + it->prefix_offset += bytes; + it->prefix_len -= bytes; + if (it->prefix_len) + return false; + } else if (it->bio_len) { + BUG_ON(bytes > bio_iter_len(it->bio, it->iter)); + bio_advance_iter(it->bio, &it->iter, bytes); + it->bio_len -= bytes; + if (!cursor->resid) { + BUG_ON(!cursor->last_piece); + return false; /* no more data */ + } + if (it->iter.bi_size && it->iter.bi_bvec_done) + return false; /* more bytes to process in this segment */ + + if (it->bio_len && !it->iter.bi_size) { + it->bio = it->bio->bi_next; + it->iter = it->bio->bi_iter; + if (cursor->resid < it->iter.bi_size) + it->iter.bi_size = cursor->resid; + } + } else { + BUG_ON(!it->suffix_len); + it->suffix_offset += bytes; + it->suffix_len -= bytes; + if (it->suffix_len) + return false; + } if (!cursor->resid) { BUG_ON(!cursor->last_piece); return false; /* no more data */ } - if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done)) - return false; /* more bytes to process in this segment */ - - if (!it->iter.bi_size) { - it->bio = it->bio->bi_next; - it->iter = it->bio->bi_iter; - if (cursor->resid < it->iter.bi_size) - it->iter.bi_size = cursor->resid; + BUG_ON(cursor->last_piece); + if (it->suffix_len) { + cursor->last_piece = cursor->resid == it->suffix_len; + } else if (it->bio_len) { + cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); + } else if (it->prefix_len) { + cursor->last_piece = cursor->resid == it->prefix_len; + } else { + BUG(); } - BUG_ON(cursor->last_piece); - BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); - cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); return true; } #endif /* CONFIG_BLOCK */ From patchwork Mon Dec 3 12:50:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709495 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 65E5913BF for ; Mon, 3 Dec 2018 12:56:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 538D12B408 for ; Mon, 3 Dec 2018 12:56:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4754A2B4AF; Mon, 3 Dec 2018 12:56:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8764F2B408 for ; Mon, 3 Dec 2018 12:56:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726552AbeLCM5D (ORCPT ); Mon, 3 Dec 2018 07:57:03 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22066 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726519AbeLCM5C (ORCPT ); Mon, 3 Dec 2018 07:57:02 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S4; Mon, 03 Dec 2018 20:50:40 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 02/11] libceph: support op append Date: Mon, 3 Dec 2018 07:50:26 -0500 Message-Id: <1543841435-13652-3-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxWFW5AFW8Zry7JFW5Zw43Wrg_yoWrJFWUpF ZrA3yjyFW3Ja4xZrs7GFZ5t3yrJ3yvyF42qrWDKrs3Can3JrykZ3Z8Xr9Fgr1UZF4Fg348 CF1Y9r90qw1SvrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbiOJrUUUUU= X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiHAASelpcg+P31gAAsz Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP we need to send append operation when we want to support journaling in kernel client. Signed-off-by: Dongsheng Yang --- net/ceph/osd_client.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/net/ceph/osd_client.c b/net/ceph/osd_client.c index d23a9f8..ffc93ee 100644 --- a/net/ceph/osd_client.c +++ b/net/ceph/osd_client.c @@ -386,6 +386,7 @@ static void osd_req_op_data_release(struct ceph_osd_request *osd_req, case CEPH_OSD_OP_READ: case CEPH_OSD_OP_WRITE: case CEPH_OSD_OP_WRITEFULL: + case CEPH_OSD_OP_APPEND: ceph_osd_data_release(&op->extent.osd_data); break; case CEPH_OSD_OP_CALL: @@ -702,6 +703,7 @@ static void get_num_data_items(struct ceph_osd_request *req, /* request */ case CEPH_OSD_OP_WRITE: case CEPH_OSD_OP_WRITEFULL: + case CEPH_OSD_OP_APPEND: case CEPH_OSD_OP_SETXATTR: case CEPH_OSD_OP_CMPXATTR: case CEPH_OSD_OP_NOTIFY_ACK: @@ -787,13 +789,13 @@ void osd_req_op_extent_init(struct ceph_osd_request *osd_req, BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO && - opcode != CEPH_OSD_OP_TRUNCATE); + opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_APPEND); op->extent.offset = offset; op->extent.length = length; op->extent.truncate_size = truncate_size; op->extent.truncate_seq = truncate_seq; - if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL) + if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL || opcode == CEPH_OSD_OP_APPEND) payload_len += length; op->indata_len = payload_len; @@ -815,7 +817,7 @@ void osd_req_op_extent_update(struct ceph_osd_request *osd_req, BUG_ON(length > previous); op->extent.length = length; - if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) + if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL || op->op == CEPH_OSD_OP_APPEND) op->indata_len -= previous - length; } EXPORT_SYMBOL(osd_req_op_extent_update); @@ -837,7 +839,7 @@ void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req, op->extent.offset += offset_inc; op->extent.length -= offset_inc; - if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) + if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL || op->op == CEPH_OSD_OP_APPEND) op->indata_len -= offset_inc; } EXPORT_SYMBOL(osd_req_op_extent_dup_last); @@ -977,6 +979,7 @@ static u32 osd_req_encode_op(struct ceph_osd_op *dst, case CEPH_OSD_OP_READ: case CEPH_OSD_OP_WRITE: case CEPH_OSD_OP_WRITEFULL: + case CEPH_OSD_OP_APPEND: case CEPH_OSD_OP_ZERO: case CEPH_OSD_OP_TRUNCATE: dst->extent.offset = cpu_to_le64(src->extent.offset); @@ -1070,7 +1073,7 @@ struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE && - opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE); + opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_APPEND); req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool, GFP_NOFS); @@ -1944,6 +1947,7 @@ static void setup_request_data(struct ceph_osd_request *req) /* request */ case CEPH_OSD_OP_WRITE: case CEPH_OSD_OP_WRITEFULL: + case CEPH_OSD_OP_APPEND: WARN_ON(op->indata_len != op->extent.length); ceph_osdc_msg_data_add(request_msg, &op->extent.osd_data); From patchwork Mon Dec 3 12:50:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709515 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 327F516B1 for ; Mon, 3 Dec 2018 12:57:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2136A2B408 for ; Mon, 3 Dec 2018 12:57:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 157D02B4AF; Mon, 3 Dec 2018 12:57:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 11EFF2B408 for ; Mon, 3 Dec 2018 12:57:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726602AbeLCM55 (ORCPT ); Mon, 3 Dec 2018 07:57:57 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22076 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726553AbeLCM55 (ORCPT ); Mon, 3 Dec 2018 07:57:57 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S5; Mon, 03 Dec 2018 20:50:40 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 03/11] libceph: introduce cls_journaler_client Date: Mon, 3 Dec 2018 07:50:27 -0500 Message-Id: <1543841435-13652-4-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S5 X-Coremail-Antispam: 1Uf129KBjvAXoWfGryxWFWrWw4rAFW5Jr1UJrb_yoW8WFyDuo W2kr4UGrn5GFWDZrWvkrn2gFyjgayrK3Z5Cw1YqFsruanrZ34fKw47Ga13ta45CF4ayrsr Kw4xJ3WfJr48A3W7n29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUpgd1UUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiEQASelnxsQkyQQAAsa Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a cls client module for journaler. Signed-off-by: Dongsheng Yang --- include/linux/ceph/cls_journaler_client.h | 89 +++++ net/ceph/cls_journaler_client.c | 535 ++++++++++++++++++++++++++++++ 2 files changed, 624 insertions(+) create mode 100644 include/linux/ceph/cls_journaler_client.h create mode 100644 net/ceph/cls_journaler_client.c diff --git a/include/linux/ceph/cls_journaler_client.h b/include/linux/ceph/cls_journaler_client.h new file mode 100644 index 0000000..c6b195c --- /dev/null +++ b/include/linux/ceph/cls_journaler_client.h @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_CEPH_CLS_JOURNAL_CLIENT_H +#define _LINUX_CEPH_CLS_JOURNAL_CLIENT_H + +#include + +struct ceph_journaler; +struct ceph_journaler_client; + +struct ceph_journaler_object_pos { + struct list_head node; + u64 object_num; + u64 tag_tid; + u64 entry_tid; +}; + +struct ceph_journaler_client { + struct list_head node; + size_t id_len; + char *id; + size_t data_len; + char *data; + struct list_head object_positions; +}; + +struct ceph_journaler_tag { + uint64_t tid; + uint64_t tag_class; + size_t data_len; + char *data; +}; + +void destroy_client(struct ceph_journaler_client *client); + +int ceph_cls_journaler_get_immutable_metas(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint8_t *order, + uint8_t *splay_width, + int64_t *pool_id); + +int ceph_cls_journaler_get_mutable_metas(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t *minimum_set, uint64_t *active_set); + +int ceph_cls_journaler_client_list(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + struct ceph_journaler_client **clients, + uint32_t *client_num); + +int ceph_cls_journaler_get_next_tag_tid(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t *tag_tid); + +int ceph_cls_journaler_get_tag(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t tag_tid, struct ceph_journaler_tag *tag); + +int ceph_cls_journaler_tag_create(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t tag_tid, uint64_t tag_class, + void *buf, uint32_t buf_len); + +int ceph_cls_journaler_client_committed(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + struct ceph_journaler_client *client, + struct list_head *object_positions); + +int ceph_cls_journaler_set_active_set(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t active_set); + +int ceph_cls_journaler_set_minimum_set(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t minimum_set); + +int ceph_cls_journaler_guard_append(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t soft_limit); +#endif diff --git a/net/ceph/cls_journaler_client.c b/net/ceph/cls_journaler_client.c new file mode 100644 index 0000000..626dd87 --- /dev/null +++ b/net/ceph/cls_journaler_client.c @@ -0,0 +1,535 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +#include + +//TODO get all metas in one single request +int ceph_cls_journaler_get_immutable_metas(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint8_t *order, + uint8_t *splay_width, + int64_t *pool_id) +{ + struct page *reply_page; + size_t reply_len = sizeof(*order); + int ret = 0; + + reply_page = alloc_page(GFP_NOIO); + if (!reply_page) + return -ENOMEM; + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_order", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(order, page_address(reply_page), reply_len); + } else { + goto out; + } + + reply_len = sizeof(*splay_width); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_splay_width", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(splay_width, page_address(reply_page), reply_len); + } else { + goto out; + } + + reply_len = sizeof(*pool_id); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_pool_id", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(pool_id, page_address(reply_page), reply_len); + } else { + goto out; + } +out: + __free_page(reply_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_get_immutable_metas); + +//TODO get all metas in one single request +int ceph_cls_journaler_get_mutable_metas(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t *minimum_set, uint64_t *active_set) +{ + struct page *reply_page; + int ret = 0; + size_t reply_len = sizeof(*minimum_set); + + reply_page = alloc_page(GFP_NOIO); + if (!reply_page) + return -ENOMEM; + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_minimum_set", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(minimum_set, page_address(reply_page), reply_len); + } else { + goto out; + } + + reply_len = sizeof(active_set); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_active_set", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(active_set, page_address(reply_page), reply_len); + } else { + goto out; + } +out: + __free_page(reply_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_get_mutable_metas); + +static int decode_object_position(void **p, void *end, struct ceph_journaler_object_pos *pos) +{ + u8 struct_v; + u32 struct_len; + int ret = 0; + + u64 object_num = 0; + u64 tag_tid = 0; + u64 entry_tid = 0; + + ret = ceph_start_decoding(p, end, 1, "cls_journal_object_position", + &struct_v, &struct_len); + if (ret) + return ret; + + object_num = ceph_decode_64(p); + + tag_tid = ceph_decode_64(p); + + entry_tid = ceph_decode_64(p); + + pos->object_num = object_num; + pos->tag_tid = tag_tid; + pos->entry_tid = entry_tid; + + return ret; +} + +void destroy_client(struct ceph_journaler_client *client) +{ + struct ceph_journaler_object_pos *pos, *next; + + list_for_each_entry_safe(pos, next, &client->object_positions, node) { + list_del(&pos->node); + kfree(pos); + } + kfree(client->id); + kfree(client->data); +} + +static int decode_client(void **p, void *end, struct ceph_journaler_client *client) +{ + u8 struct_v; + u8 state_raw; + u32 struct_len; + int ret = 0, num = 0, i = 0; + struct ceph_journaler_object_pos *pos, *next_pos; + + INIT_LIST_HEAD(&client->node); + INIT_LIST_HEAD(&client->object_positions); + ret = ceph_start_decoding(p, end, 1, "cls_journal_get_client_reply", + &struct_v, &struct_len); + if (ret) + return ret; + + client->id = ceph_extract_encoded_string(p, end, &client->id_len, GFP_NOIO); + if (IS_ERR(client->id)) { + ret = PTR_ERR(client->id); + goto err; + } + client->data = ceph_extract_encoded_string(p, end, &client->data_len, GFP_NOIO); + if (IS_ERR(client->data)) { + ret = PTR_ERR(client->data); + goto free_id; + } + ret = ceph_start_decoding(p, end, 1, "cls_joural_client_object_set_position", + &struct_v, &struct_len); + if (ret) + goto free_data; + + num = ceph_decode_32(p); + for (i = 0; i < num; i++) { + pos = kzalloc(sizeof(*pos), GFP_KERNEL); + if (!pos) + goto free_positions; + + ret = decode_object_position(p, end, pos); + if (ret) { + kfree(pos); + goto free_positions; + } + list_add_tail(&pos->node, &client->object_positions); + } + + state_raw = ceph_decode_8(p); + return 0; + +free_positions: + list_for_each_entry_safe(pos, next_pos, &client->object_positions, node) { + list_del(&pos->node); + kfree(pos); + } +free_data: + kfree(client->data); +free_id: + kfree(client->id); +err: + return ret; +} + +static int decode_clients(void **p, void *end, struct ceph_journaler_client **clients, uint32_t *client_num) +{ + int i; + int ret = 0; + + *client_num = ceph_decode_32(p); + if (ret) + return ret; + + *clients = kcalloc(*client_num, sizeof(**clients), GFP_NOIO); + if (!*clients) + return -ENOMEM; + + for (i = 0; i < *client_num; i++) { + ret = decode_client(p, end, *clients + i); + if (ret) + goto err; + } + return 0; + +err: + for (--i; i >= 0; i--) + destroy_client(*clients + i); + kfree(*clients); + + return ret; +} + +int ceph_cls_journaler_client_list(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + struct ceph_journaler_client **clients, + uint32_t *client_num) +{ + struct page *reply_page; + struct page *req_page; + int ret = 0; + size_t reply_len = PAGE_SIZE; + int buf_size; + void *p, *end; + char name[] = ""; + + buf_size = strlen(name) + sizeof(__le32) + sizeof(uint64_t); + + if (buf_size > PAGE_SIZE) + return -E2BIG; + + reply_page = alloc_page(GFP_NOIO); + if (!reply_page) + return -ENOMEM; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) { + ret = -ENOMEM; + goto free_reply_page; + } + + p = page_address(req_page); + end = p + buf_size; + + ceph_encode_string(&p, end, name, strlen(name)); + ceph_encode_64(&p, (uint64_t)256); + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "client_list", + CEPH_OSD_FLAG_READ, req_page, + buf_size, reply_page, &reply_len); + + if (!ret) { + p = page_address(reply_page); + end = p + reply_len; + + ret = decode_clients(&p, end, clients, client_num); + } + + __free_page(req_page); +free_reply_page: + __free_page(reply_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_client_list); + +int ceph_cls_journaler_get_next_tag_tid(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t *tag_tid) +{ + struct page *reply_page; + int ret = 0; + size_t reply_len = PAGE_SIZE; + + reply_page = alloc_page(GFP_NOIO); + if (!reply_page) + return -ENOMEM; + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_next_tag_tid", + CEPH_OSD_FLAG_READ, NULL, + 0, reply_page, &reply_len); + + if (!ret) { + memcpy(tag_tid, page_address(reply_page), reply_len); + } + + __free_page(reply_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_get_next_tag_tid); + +int ceph_cls_journaler_tag_create(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t tag_tid, uint64_t tag_class, + void *buf, uint32_t buf_len) +{ + struct page *req_page; + int ret = 0; + int buf_size; + void *p, *end; + + buf_size = buf_len + sizeof(__le32) + sizeof(uint64_t) + sizeof(uint64_t); + + if (buf_size > PAGE_SIZE) + return -E2BIG; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + return -ENOMEM; + + p = page_address(req_page); + end = p + buf_size; + + ceph_encode_64(&p, tag_tid); + ceph_encode_64(&p, tag_class); + ceph_encode_string(&p, end, buf, buf_len); + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "tag_create", + CEPH_OSD_FLAG_WRITE, req_page, + buf_size, NULL, NULL); + + __free_page(req_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_tag_create); + +int decode_tag(void **p, void *end, struct ceph_journaler_tag *tag) +{ + int ret = 0; + u8 struct_v; + u32 struct_len; + + ret = ceph_start_decoding(p, end, 1, "cls_journaler_tag", + &struct_v, &struct_len); + if (ret) + return ret; + + tag->tid = ceph_decode_64(p); + tag->tag_class = ceph_decode_64(p); + tag->data = ceph_extract_encoded_string(p, end, &tag->data_len, GFP_NOIO); + + return 0; +} + +int ceph_cls_journaler_get_tag(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t tag_tid, struct ceph_journaler_tag *tag) +{ + struct page *reply_page; + struct page *req_page; + int ret = 0; + size_t reply_len = PAGE_SIZE; + int buf_size; + void *p, *end; + + buf_size = sizeof(tag_tid); + + reply_page = alloc_page(GFP_NOIO); + if (!reply_page) + return -ENOMEM; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + goto free_reply_page; + + p = page_address(req_page); + end = p + buf_size; + + ceph_encode_64(&p, tag_tid); + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "get_tag", + CEPH_OSD_FLAG_READ, req_page, + buf_size, reply_page, &reply_len); + + if (!ret) { + p = page_address(reply_page); + end = p + reply_len; + + ret = decode_tag(&p, end, tag); + } + + __free_page(req_page); +free_reply_page: + __free_page(reply_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_get_tag); + +static int version_len = 6; + +int ceph_cls_journaler_client_committed(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + struct ceph_journaler_client *client, + struct list_head *object_positions) +{ + struct page *req_page; + int ret = 0; + int buf_size; + void *p, *end; + struct ceph_journaler_object_pos *position = NULL; + + int object_position_len = version_len + 8 + 8 + 8; + + int pos_num = 0; + + buf_size = 4 + client->id_len + version_len + 4; + + list_for_each_entry(position, object_positions, node) { + buf_size += object_position_len; + pos_num++; + } + + if (buf_size > PAGE_SIZE) + return -E2BIG; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + return -ENOMEM; + + p = page_address(req_page); + end = p + buf_size; + + ceph_encode_string(&p, end, client->id, client->id_len); + + ceph_start_encoding(&p, 1, 1, buf_size - client->id_len - version_len - 4); + + ceph_encode_32(&p, pos_num); + + list_for_each_entry(position, object_positions, node) { + ceph_start_encoding(&p, 1, 1, 24); + ceph_encode_64(&p, position->object_num); + ceph_encode_64(&p, position->tag_tid); + ceph_encode_64(&p, position->entry_tid); + } + + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "client_commit", + CEPH_OSD_FLAG_WRITE, req_page, + buf_size, NULL, NULL); + + __free_page(req_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_client_committed); + + +int ceph_cls_journaler_set_minimum_set(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t minimum_set) +{ + struct page *req_page; + int ret = 0; + void *p; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + return -ENOMEM; + + p = page_address(req_page); + ceph_encode_64(&p, minimum_set); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "set_minimum_set", + CEPH_OSD_FLAG_WRITE, req_page, + 8, NULL, NULL); + + __free_page(req_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_set_minimum_set); + +int ceph_cls_journaler_set_active_set(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t active_set) +{ + struct page *req_page; + int ret = 0; + void *p; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + return -ENOMEM; + + p = page_address(req_page); + ceph_encode_64(&p, active_set); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "set_active_set", + CEPH_OSD_FLAG_WRITE, req_page, + 8, NULL, NULL); + + __free_page(req_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_set_active_set); + +int ceph_cls_journaler_guard_append(struct ceph_osd_client *osdc, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + uint64_t soft_limit) +{ + struct page *req_page; + int ret = 0; + void *p; + + req_page = alloc_page(GFP_NOIO); + if (!req_page) + return -ENOMEM; + + p = page_address(req_page); + ceph_encode_64(&p, soft_limit); + ret = ceph_osdc_call(osdc, oid, oloc, "journal", "guard_append", + CEPH_OSD_FLAG_READ, req_page, + 8, NULL, NULL); + + __free_page(req_page); + return ret; +} +EXPORT_SYMBOL(ceph_cls_journaler_guard_append); From patchwork Mon Dec 3 12:50:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709513 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2063116B1 for ; Mon, 3 Dec 2018 12:57:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FE452B408 for ; Mon, 3 Dec 2018 12:57:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 044512B4AF; Mon, 3 Dec 2018 12:57:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE0C82B408 for ; Mon, 3 Dec 2018 12:57:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726600AbeLCM5z (ORCPT ); Mon, 3 Dec 2018 07:57:55 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22071 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726451AbeLCM5y (ORCPT ); Mon, 3 Dec 2018 07:57:54 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S6; Mon, 03 Dec 2018 20:50:40 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 04/11] libceph: introduce generic journaling Date: Mon, 3 Dec 2018 07:50:28 -0500 Message-Id: <1543841435-13652-5-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S6 X-Coremail-Antispam: 1Uf129KBjvAXoWfAryrJFyDZFW8GrykGr1rWFg_yoW8ur4Dto Z7Xr1DCF1rGa47ZFWkKrn7J34rZayUJayrAr4YqF4Y93Z3Jry0v3y7Gr15tFyYv3yUZrsF qw4xJwn3Jw4kA3WUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUpgd1UUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiGwESelpcgQPvqwAAsy Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This commit introduce the generic journaling. This is a initial commit, which only includes some generic functions, such as journaler_create|destroy() and journaler_open|close(). Signed-off-by: Dongsheng Yang --- include/linux/ceph/journaler.h | 162 +++++++++++++ net/ceph/Makefile | 3 +- net/ceph/journaler.c | 513 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 677 insertions(+), 1 deletion(-) create mode 100644 include/linux/ceph/journaler.h create mode 100644 net/ceph/journaler.c diff --git a/include/linux/ceph/journaler.h b/include/linux/ceph/journaler.h new file mode 100644 index 0000000..50e8c52 --- /dev/null +++ b/include/linux/ceph/journaler.h @@ -0,0 +1,162 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _FS_CEPH_JOURNAL_H +#define _FS_CEPH_JOURNAL_H + +#include +#include + +struct ceph_osd_client; + +#define JOURNAL_HEADER_PREFIX "journal." +#define JOURNAL_OBJECT_PREFIX "journal_data." + +#define LOCAL_MIRROR_UUID "" + +/// preamble, version, entry tid, tag id +static const uint32_t HEADER_FIXED_SIZE = 25; +/// data size, crc +static const uint32_t REMAINDER_FIXED_SIZE = 8; +static const uint64_t PREAMBLE = 0x3141592653589793; + +struct ceph_journaler_ctx; +typedef void (*ceph_journalecallback_t)(struct ceph_journaler_ctx *); + +struct ceph_journaler_ctx { + struct list_head node; + struct ceph_bio_iter *bio_iter; + int result; + + void *priv; + ceph_journalecallback_t callback; +}; + +struct ceph_journaler_future { + uint64_t tag_tid; + uint64_t entry_tid; + uint64_t commit_tid; + + bool safe; + bool consistent; + + struct ceph_journaler_ctx *ctx; + struct ceph_journaler_future *wait; +}; + +struct ceph_journaler_entry { + struct list_head node; + uint64_t tag_tid; + uint64_t entry_tid; + ssize_t data_len; + char *data; + struct ceph_bio_iter *bio_iter; +}; + +struct entry_tid { + struct list_head node; + uint64_t tag_tid; + uint64_t entry_tid; +}; + +struct commit_entry { + struct rb_node r_node; + uint64_t commit_tid; + uint64_t object_num; + uint64_t tag_tid; + uint64_t entry_tid; + bool committed; +}; + +struct object_recorder { + spinlock_t lock; + bool overflowed; + uint64_t splay_offset; + uint64_t inflight_append; + + struct list_head append_list; + struct list_head overflow_list; +}; + +struct object_replayer { + spinlock_t lock; + uint64_t object_num; + struct ceph_journaler_object_pos *pos; + struct list_head entry_list; +}; + +struct watch_cb_data { + struct list_head node; + u64 notify_id; + u64 cookie; +}; + +struct ceph_journaler { + struct ceph_osd_client *osdc; + struct ceph_object_locator data_oloc; + + struct ceph_object_id header_oid; + struct ceph_object_locator header_oloc; + + char *object_oid_prefix; + char *client_id; + + struct object_recorder *obj_recorders; + struct object_replayer *obj_replayers; + uint64_t splay_offset; + uint64_t active_tag_tid; + + spinlock_t meta_lock; + bool closing; + uint8_t order; + uint8_t splay_width; + int64_t pool_id; + struct ceph_journaler_future *prev_future; + struct list_head watch_cb_list; + + uint64_t commit_tid; + uint64_t minimum_set; + uint64_t active_set; + + struct list_head clients; + struct ceph_journaler_client *client; + struct ceph_journaler_client *clients_array; + struct list_head object_positions_pending; + + spinlock_t commit_lock; + struct rb_root commit_entries; + spinlock_t entry_tid_lock; + struct list_head entry_tids; + spinlock_t finish_lock; + struct list_head ctx_list; + spinlock_t advancing_lock; + bool advancing; + + struct workqueue_struct *task_wq; + struct workqueue_struct *finish_wq; + struct work_struct notify_update_work; + struct work_struct commit_work; + struct work_struct finish_work; + struct work_struct overflow_work; + struct work_struct flush_work; + struct work_struct watch_cb_work; + + int (*handle_entry)(void *entry_handler, + struct ceph_journaler_entry *entry, + uint64_t commit_tid); + void *entry_handler; + + struct ceph_osd_linger_request *watch_handle; +}; + +// generic functions +struct ceph_journaler *ceph_journaler_create(struct ceph_osd_client *osdc, + struct ceph_object_locator*_oloc, + const char *journal_id, + const char *client_id); +void ceph_journaler_destroy(struct ceph_journaler *journal); + +int ceph_journaler_open(struct ceph_journaler *journal); +void ceph_journaler_close(struct ceph_journaler *journal); + +int ceph_journaler_get_cached_client(struct ceph_journaler *journaler, char *client_id, + struct ceph_journaler_client **client_result); +#endif diff --git a/net/ceph/Makefile b/net/ceph/Makefile index db09defe..a965d64 100644 --- a/net/ceph/Makefile +++ b/net/ceph/Makefile @@ -14,4 +14,5 @@ libceph-y := ceph_common.o messenger.o msgpool.o buffer.o pagelist.o \ crypto.o armor.o \ auth_x.o \ ceph_fs.o ceph_strings.o ceph_hash.o \ - pagevec.o snapshot.o string_table.o + pagevec.o snapshot.o string_table.o \ + journaler.o cls_journaler_client.o diff --git a/net/ceph/journaler.c b/net/ceph/journaler.c new file mode 100644 index 0000000..8f2ed41 --- /dev/null +++ b/net/ceph/journaler.c @@ -0,0 +1,513 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include + +#include +#include + +static char *object_oid_prefix(int pool_id, const char *journal_id) +{ + char *prefix = NULL; + ssize_t len = snprintf(NULL, 0, "%s%d.%s.", JOURNAL_OBJECT_PREFIX, + pool_id, journal_id); + + prefix = kzalloc(len + 1, GFP_KERNEL); + + if (!prefix) + return NULL; + + WARN_ON(snprintf(prefix, len + 1, "%s%d.%s.", JOURNAL_OBJECT_PREFIX, + pool_id, journal_id) != len); + + return prefix; +} + +static void watch_cb_func(struct work_struct *work); + +struct ceph_journaler *ceph_journaler_create(struct ceph_osd_client *osdc, + struct ceph_object_locator *oloc, + const char *journal_id, + const char *client_id) +{ + struct ceph_journaler *journaler = NULL; + int ret = 0; + + journaler = kzalloc(sizeof(struct ceph_journaler), GFP_KERNEL); + if (!journaler) + return NULL; + + ceph_oid_init(&journaler->header_oid); + ret = ceph_oid_aprintf(&journaler->header_oid, GFP_KERNEL, "%s%s", + JOURNAL_HEADER_PREFIX, journal_id); + if (ret) { + pr_err("aprintf error : %d", ret); + goto err_free_journaler; + } + + journaler->client_id = kstrdup(client_id, GFP_KERNEL); + if (!journaler->client_id) { + ret = -ENOMEM; + goto err_free_header_oid;; + } + + journaler->task_wq = alloc_ordered_workqueue("journaler-tasks", + WQ_MEM_RECLAIM); + if (!journaler->task_wq) + goto err_free_client_id; + + + journaler->finish_wq = alloc_ordered_workqueue("journaler-finish", + WQ_MEM_RECLAIM); + if (!journaler->finish_wq) + goto err_destroy_task_wq; + + journaler->notify_wq = alloc_ordered_workqueue("journaler-notify", + WQ_MEM_RECLAIM); + if (!journaler->notify_wq) + goto err_destroy_finish_wq; + ceph_oloc_init(&journaler->header_oloc); + ceph_oloc_copy(&journaler->header_oloc, oloc); + journaler->object_oid_prefix = object_oid_prefix(journaler->header_oloc.pool, + journal_id); + + if (!journaler->object_oid_prefix) + goto err_destroy_header_oloc; + + journaler->osdc = osdc; + ceph_oloc_init(&journaler->data_oloc); + INIT_LIST_HEAD(&journaler->clients); + INIT_LIST_HEAD(&journaler->entry_tids); + INIT_LIST_HEAD(&journaler->object_positions_pending); + INIT_LIST_HEAD(&journaler->ctx_list); + INIT_LIST_HEAD(&journaler->watch_cb_list); + journaler->commit_entries = RB_ROOT; + journaler->commit_tid = 0; + journaler->client = NULL; + journaler->clients_array = NULL; + journaler->prev_future = NULL; + journaler->closing = false; + + spin_lock_init(&journaler->meta_lock); + spin_lock_init(&journaler->entry_tid_lock); + spin_lock_init(&journaler->commit_lock); + spin_lock_init(&journaler->finish_lock); + + INIT_WORK(&journaler->watch_cb_work, watch_cb_func); + + return journaler; + +err_destroy_header_oloc: + ceph_oloc_destroy(&journaler->header_oloc); + destroy_workqueue(journaler->notify_wq); +err_destroy_finish_wq: + destroy_workqueue(journaler->finish_wq); +err_destroy_task_wq: + destroy_workqueue(journaler->task_wq); +err_free_client_id: + kfree(journaler->client_id); +err_free_header_oid: + ceph_oid_destroy(&journaler->header_oid); +err_free_journaler: + kfree(journaler); + return NULL; +} +EXPORT_SYMBOL(ceph_journaler_create); + +void ceph_journaler_destroy(struct ceph_journaler *journaler) +{ + ceph_oloc_destroy(&journaler->data_oloc); + kfree(journaler->object_oid_prefix); + ceph_oloc_destroy(&journaler->header_oloc); + destroy_workqueue(journaler->notify_wq); + destroy_workqueue(journaler->finish_wq); + destroy_workqueue(journaler->task_wq); + kfree(journaler->client_id); + ceph_oid_destroy(&journaler->header_oid); + kfree(journaler); +} +EXPORT_SYMBOL(ceph_journaler_destroy); + +static int refresh(struct ceph_journaler *journaler, bool init) +{ + int ret = 0; + int i = 0; + uint32_t client_num = 0; + struct ceph_journaler_client *clients = NULL; + struct ceph_journaler_client *client, *next; + uint64_t minimum_commit_set; + uint64_t minimum_set; + uint64_t active_set; + bool need_advance = false; + + ret = ceph_cls_journaler_get_mutable_metas(journaler->osdc, + &journaler->header_oid, &journaler->header_oloc, + &minimum_set, &active_set); + if (ret) + return ret; + + ret = ceph_cls_journaler_client_list(journaler->osdc, &journaler->header_oid, + &journaler->header_oloc, &clients, &client_num); + if (ret) + return ret; + + spin_lock(&journaler->meta_lock); + // update clients. + list_for_each_entry_safe(client, next, &journaler->clients, node) { + list_del(&client->node); + destroy_client(client); + } + if (journaler->clients_array != NULL) + kfree(journaler->clients_array); + + journaler->clients_array = clients; + for (i = 0; i < client_num; i++) { + struct ceph_journaler_client *client = &clients[i]; + + list_add_tail(&client->node, &journaler->clients); + if (!strcmp(client->id, journaler->client_id)) { + journaler->client = client; + } + } + + if (init) { + journaler->active_set = active_set; + journaler->minimum_set = minimum_set; + } else { + // check for advance active_set. + need_advance = active_set > journaler->active_set; + journaler->minimum_set = minimum_set; + } + + // calculate the minimum_commit_set. + minimum_commit_set = journaler->active_set; + list_for_each_entry(client, &journaler->clients, node) { + //TODO check the state of client + struct ceph_journaler_object_pos *pos; + list_for_each_entry(pos, &client->object_positions, node) { + uint64_t object_set = pos->object_num / journaler->splay_width; + if (object_set < minimum_commit_set) { + minimum_commit_set = object_set; + } + } + } + spin_unlock(&journaler->meta_lock); + + return 0; + +} + +static void watch_cb_func(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, struct ceph_journaler, + watch_cb_work); + LIST_HEAD(tmp_cb_data_list); + struct watch_cb_data *data, *next_data; + int ret; + + spin_lock(&journaler->meta_lock); + list_splice_tail_init(&journaler->watch_cb_list, &tmp_cb_data_list); + spin_unlock(&journaler->meta_lock); + + list_for_each_entry_safe(data, next_data, &tmp_cb_data_list, node) { + list_del(&data->node); + ret = ceph_osdc_notify_ack(journaler->osdc, &journaler->header_oid, + &journaler->header_oloc, data->notify_id, + data->cookie, NULL, 0); + + kfree(data); + if (ret) + pr_err("acknowledge_notify failed: %d", ret); + } + + ret = refresh(journaler, false); + if (ret < 0) { + pr_err("%s: failed to refresh journaler: %d", __func__, ret); + } +} + +static void journaler_watch_cb(void *arg, u64 notify_id, u64 cookie, + u64 notifier_id, void *data, size_t data_len) +{ + struct ceph_journaler *journaler = arg; + struct watch_cb_data *cb_data = NULL; + + cb_data = kzalloc(sizeof(struct watch_cb_data), GFP_KERNEL); + if (!cb_data) + return; + + cb_data->notify_id = notify_id; + cb_data->cookie = cookie; + INIT_LIST_HEAD(&cb_data->node); + + spin_lock(&journaler->meta_lock); + if (journaler->closing) { + kfree(cb_data); + spin_unlock(&journaler->meta_lock); + return; + } + list_add_tail(&journaler->watch_cb_list, &cb_data->node); + queue_work(journaler->finish_wq, &journaler->watch_cb_work); + spin_unlock(&journaler->meta_lock); +} + +static void journaler_watch_errcb(void *arg, u64 cookie, int err) +{ + pr_err("journaler watch error: %d", err); +} + +static int journaler_watch(struct ceph_journaler *journaler) +{ + struct ceph_osd_client *osdc = journaler->osdc; + struct ceph_osd_linger_request *handle; + + handle = ceph_osdc_watch(osdc, &journaler->header_oid, + &journaler->header_oloc, journaler_watch_cb, + journaler_watch_errcb, journaler); + if (IS_ERR(handle)) + return PTR_ERR(handle); + + journaler->watch_handle = handle; + return 0; +} + +static void journaler_unwatch(struct ceph_journaler *journaler) +{ + struct ceph_osd_client *osdc = journaler->osdc; + int ret = 0; + + ret = ceph_osdc_unwatch(osdc, journaler->watch_handle); + if (ret) + pr_err("%s: failed to unwatch: %d", __func__, ret); + + journaler->watch_handle = NULL; +} + +static int copy_object_pos(struct ceph_journaler_object_pos *pos, + struct ceph_journaler_object_pos **new_pos) +{ + struct ceph_journaler_object_pos *temp_pos; + + temp_pos = kzalloc(sizeof(*temp_pos), GFP_KERNEL); + if (temp_pos == NULL) { + return -ENOMEM; + } + INIT_LIST_HEAD(&temp_pos->node); + temp_pos->object_num = pos->object_num; + temp_pos->tag_tid = pos->tag_tid; + temp_pos->entry_tid = pos->entry_tid; + + *new_pos = temp_pos; + + return 0; +} + +int ceph_journaler_open(struct ceph_journaler *journaler) +{ + uint8_t order, splay_width; + int64_t pool_id; + int i = 0, ret = 0; + struct ceph_journaler_object_pos *pos = NULL, *next_pos = NULL; + struct ceph_journaler_client *client = NULL, *next_client = NULL; + + ret = ceph_cls_journaler_get_immutable_metas(journaler->osdc, + &journaler->header_oid, + &journaler->header_oloc, + &order, + &splay_width, + &pool_id); + if (ret) { + pr_err("failed to get immutable metas.");; + goto out; + } + + spin_lock(&journaler->meta_lock); + // set the immutable metas. + journaler->order = order; + journaler->splay_width = splay_width; + journaler->pool_id = pool_id; + + if (journaler->pool_id == -1) { + ceph_oloc_copy(&journaler->data_oloc, &journaler->header_oloc); + journaler->pool_id = journaler->data_oloc.pool; + } else { + journaler->data_oloc.pool = journaler->pool_id; + } + + // initialize ->obj_recorders and ->obj_replayers. + journaler->obj_recorders = kzalloc(sizeof(struct object_recorder) * + journaler->splay_width, GFP_KERNEL); + + if (!journaler->obj_recorders) { + spin_unlock(&journaler->meta_lock); + goto out; + } + + journaler->obj_replayers = kzalloc(sizeof(struct object_replayer) * + journaler->splay_width, GFP_KERNEL); + + if (!journaler->obj_replayers) { + spin_unlock(&journaler->meta_lock); + goto free_recorders; + } + + for (i = 0; i < journaler->splay_width; i++) { + struct object_recorder *obj_recorder = &journaler->obj_recorders[i]; + struct object_replayer *obj_replayer = &journaler->obj_replayers[i]; + + spin_lock_init(&obj_recorder->lock); + obj_recorder->overflowed = false; + obj_recorder->splay_offset = i; + obj_recorder->inflight_append = 0; + INIT_LIST_HEAD(&obj_recorder->append_list); + INIT_LIST_HEAD(&obj_recorder->overflow_list); + + spin_lock_init(&obj_replayer->lock); + obj_replayer->object_num = i; + obj_replayer->pos = NULL; + INIT_LIST_HEAD(&obj_replayer->entry_list); + } + spin_unlock(&journaler->meta_lock); + + ret = refresh(journaler, true); + if (ret) + goto free_replayers; + + spin_lock(&journaler->meta_lock); + list_for_each_entry(pos, &journaler->client->object_positions, node) { + struct ceph_journaler_object_pos *new_pos = NULL; + + ret = copy_object_pos(pos, &new_pos); + if (ret) { + spin_unlock(&journaler->meta_lock); + goto destroy_clients; + } + + list_add_tail(&new_pos->node, &journaler->object_positions_pending); + } + spin_unlock(&journaler->meta_lock); + + ret = journaler_watch(journaler); + if (ret) { + pr_err("journaler_watch error: %d", ret); + goto free_pos_pending; + } + return 0; + +free_pos_pending: + list_for_each_entry_safe(pos, next_pos, + &journaler->object_positions_pending, node) { + list_del(&pos->node); + kfree(pos); + } +destroy_clients: + list_for_each_entry_safe(client, next_client, + &journaler->clients, node) { + list_del(&client->node); + destroy_client(client); + } + + if (journaler->clients_array != NULL) + kfree(journaler->clients_array); +free_replayers: + kfree(journaler->obj_replayers); +free_recorders: + kfree(journaler->obj_recorders); +out: + return ret; +} +EXPORT_SYMBOL(ceph_journaler_open); + +DEFINE_RB_INSDEL_FUNCS(commit_entry, struct commit_entry, commit_tid, r_node) + +void ceph_journaler_close(struct ceph_journaler *journaler) +{ + struct ceph_journaler_client *client = NULL, *next = NULL; + struct commit_entry *entry = NULL; + struct entry_tid *entry_tid = NULL, *entry_tid_next = NULL; + struct ceph_journaler_object_pos *pos = NULL, *next_pos = NULL; + struct rb_node *n; + int i = 0; + + journaler_unwatch(journaler); + + spin_lock(&journaler->meta_lock); + journaler->closing = true; + spin_unlock(&journaler->meta_lock); + + queue_work(journaler->task_wq, &journaler->flush_work); + flush_workqueue(journaler->finish_wq); + flush_workqueue(journaler->task_wq); + + list_for_each_entry_safe(pos, next_pos, + &journaler->object_positions_pending, node) { + list_del(&pos->node); + kfree(pos); + } + + list_for_each_entry_safe(client, next, &journaler->clients, node) { + list_del(&client->node); + destroy_client(client); + } + + if (journaler->clients_array != NULL) + kfree(journaler->clients_array); + + for (n = rb_first(&journaler->commit_entries); n;) { + entry = rb_entry(n, struct commit_entry, r_node); + + n = rb_next(n); + erase_commit_entry(&journaler->commit_entries, entry); + kfree(entry); + } + + for (i = 0; i < journaler->splay_width; i++) { + struct object_recorder *obj_recorder = &journaler->obj_recorders[i]; + struct object_replayer *obj_replayer = &journaler->obj_replayers[i]; + + spin_lock(&obj_recorder->lock); + BUG_ON(!list_empty(&obj_recorder->append_list) || + !list_empty(&obj_recorder->overflow_list)); + spin_unlock(&obj_recorder->lock); + + spin_lock(&obj_replayer->lock); + BUG_ON(!list_empty(&obj_replayer->entry_list)); + spin_unlock(&obj_replayer->lock); + } + + kfree(journaler->obj_recorders); + kfree(journaler->obj_replayers); + + list_for_each_entry_safe(entry_tid, entry_tid_next, + &journaler->entry_tids, node) { + list_del(&entry_tid->node); + kfree(entry_tid); + } + + WARN_ON(!list_empty(&journaler->watch_cb_list)); + + return; +} +EXPORT_SYMBOL(ceph_journaler_close); + +int ceph_journaler_get_cached_client(struct ceph_journaler *journaler, char *client_id, + struct ceph_journaler_client **client_result) +{ + struct ceph_journaler_client *client = NULL; + int ret = -ENOENT; + + list_for_each_entry(client, &journaler->clients, node) { + if (!strcmp(client->id, client_id)) { + *client_result = client; + ret = 0; + break; + } + } + + return ret; +} +EXPORT_SYMBOL(ceph_journaler_get_cached_client); From patchwork Mon Dec 3 12:50:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709503 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DF57416B1 for ; Mon, 3 Dec 2018 12:56:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF0C42B415 for ; Mon, 3 Dec 2018 12:56:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C05D72B54C; Mon, 3 Dec 2018 12:56:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D08162B415 for ; Mon, 3 Dec 2018 12:56:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726546AbeLCM5S (ORCPT ); Mon, 3 Dec 2018 07:57:18 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22075 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726548AbeLCM5S (ORCPT ); Mon, 3 Dec 2018 07:57:18 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S7; Mon, 03 Dec 2018 20:50:41 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 05/11] libceph: journaling: introduce api to replay uncommitted journal events Date: Mon, 3 Dec 2018 07:50:29 -0500 Message-Id: <1543841435-13652-6-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S7 X-Coremail-Antispam: 1Uf129KBjvAXoW3uw47XF43ur15WFy3trWxXrb_yoW8XryfWo WxuF4UC3WrGa47ury8Krn7W34Sqa48JayrArsIqF4Y93Z7A340939rGr15t34YyF4rur4D Xws7Jwn3tw4DA34Un29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfULsYwUUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiCQsSelkXOkLPiwAAsY Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we are going to make sure the data and journal are consistent in opening journal, we can call the api of start_replay() to replay the all events recorded but not committed. Signed-off-by: Dongsheng Yang --- include/linux/ceph/journaler.h | 2 + net/ceph/journaler.c | 535 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 537 insertions(+) diff --git a/include/linux/ceph/journaler.h b/include/linux/ceph/journaler.h index 50e8c52..acc1c6f 100644 --- a/include/linux/ceph/journaler.h +++ b/include/linux/ceph/journaler.h @@ -159,4 +159,6 @@ struct ceph_journaler *ceph_journaler_create(struct ceph_osd_client *osdc, int ceph_journaler_get_cached_client(struct ceph_journaler *journaler, char *client_id, struct ceph_journaler_client **client_result); +// replaying +int ceph_journaler_start_replay(struct ceph_journaler *journaler); #endif diff --git a/net/ceph/journaler.c b/net/ceph/journaler.c index 8f2ed41..3b73725 100644 --- a/net/ceph/journaler.c +++ b/net/ceph/journaler.c @@ -511,3 +511,538 @@ int ceph_journaler_get_cached_client(struct ceph_journaler *journaler, char *cli return ret; } EXPORT_SYMBOL(ceph_journaler_get_cached_client); + +// replaying +static int ceph_journaler_obj_read_sync(struct ceph_journaler *journaler, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + void *buf, uint32_t read_off, uint64_t buf_len) + +{ + struct ceph_osd_client *osdc = journaler->osdc; + struct ceph_osd_request *req; + struct page **pages; + int num_pages = calc_pages_for(0, buf_len); + int ret; + + req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); + if (!req) + return -ENOMEM; + + ceph_oid_copy(&req->r_base_oid, oid); + ceph_oloc_copy(&req->r_base_oloc, oloc); + req->r_flags = CEPH_OSD_FLAG_READ; + + pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); + if (IS_ERR(pages)) { + ret = PTR_ERR(pages); + goto out_req; + } + + osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, read_off, buf_len, 0, 0); + osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, + true); + + ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); + if (ret) + goto out_req; + + ceph_osdc_start_request(osdc, req, false); + ret = ceph_osdc_wait_request(osdc, req); + if (ret >= 0) + ceph_copy_from_page_vector(pages, buf, 0, ret); + +out_req: + ceph_osdc_put_request(req); + return ret; +} + +static bool entry_is_readable(struct ceph_journaler *journaler, void *buf, + void *end, uint32_t *bytes_needed) +{ + uint32_t remaining = end - buf; + uint64_t preamble; + uint32_t data_size; + void *origin_buf = buf; + uint32_t crc = 0, crc_encoded = 0; + + if (remaining < HEADER_FIXED_SIZE) { + *bytes_needed = HEADER_FIXED_SIZE - remaining; + return false; + } + + preamble = ceph_decode_64(&buf); + if (PREAMBLE != preamble) { + *bytes_needed = 0; + return false; + } + + buf += (HEADER_FIXED_SIZE - sizeof(preamble)); + remaining = end - buf; + if (remaining < sizeof(uint32_t)) { + *bytes_needed = sizeof(uint32_t) - remaining; + return false; + } + + data_size = ceph_decode_32(&buf); + remaining = end - buf; + if (remaining < data_size) { + *bytes_needed = data_size - remaining; + return false; + } + + buf += data_size; + + remaining = end - buf; + if (remaining < sizeof(uint32_t)) { + *bytes_needed = sizeof(uint32_t) - remaining; + return false; + } + + *bytes_needed = 0; + crc = crc32c(0, origin_buf, buf - origin_buf); + crc_encoded = ceph_decode_32(&buf); + if (crc != crc_encoded) { + pr_err("crc corrupted"); + return false; + } + return true; +} + +static int playback_entry(struct ceph_journaler *journaler, + struct ceph_journaler_entry *entry, + uint64_t commit_tid) +{ + int ret = 0; + + if (journaler->handle_entry != NULL) + ret = journaler->handle_entry(journaler->entry_handler, + entry, commit_tid); + + return ret; +} + +static bool get_last_entry_tid(struct ceph_journaler *journaler, + uint64_t tag_tid, uint64_t *entry_tid) +{ + struct entry_tid *pos = NULL; + + spin_lock(&journaler->entry_tid_lock); + list_for_each_entry(pos, &journaler->entry_tids, node) { + if (pos->tag_tid == tag_tid) { + *entry_tid = pos->entry_tid; + spin_unlock(&journaler->entry_tid_lock); + return true; + } + } + spin_unlock(&journaler->entry_tid_lock); + + return false; +} + +static int reserve_entry_tid(struct ceph_journaler *journaler, + uint64_t tag_tid, uint64_t entry_tid) +{ + struct entry_tid *pos; + + spin_lock(&journaler->entry_tid_lock); + list_for_each_entry(pos, &journaler->entry_tids, node) { + if (pos->tag_tid == tag_tid) { + if (pos->entry_tid < entry_tid) { + pos->entry_tid = entry_tid; + } + + spin_unlock(&journaler->entry_tid_lock); + return 0; + } + } + + pos = kzalloc(sizeof(struct entry_tid), GFP_KERNEL); + if (!pos) { + WARN_ON(!pos); + return -ENOMEM; + } + + pos->tag_tid = tag_tid; + pos->entry_tid = entry_tid; + INIT_LIST_HEAD(&pos->node); + + list_add_tail(&pos->node, &journaler->entry_tids); + spin_unlock(&journaler->entry_tid_lock); + + return 0; +} + +static struct ceph_journaler_entry *journaler_entry_decode(void **p, void *end) +{ + struct ceph_journaler_entry *entry = NULL; + uint64_t preamble = 0; + uint8_t version = 0; + uint32_t crc = 0, crc_encoded = 0; + void *start = *p; + + preamble = ceph_decode_64(p); + if (PREAMBLE != preamble) { + return NULL; + } + + version = ceph_decode_8(p); + + if (version != 1) + return NULL; + + entry = kzalloc(sizeof(struct ceph_journaler_entry), GFP_KERNEL); + if (!entry) { + goto err; + } + + INIT_LIST_HEAD(&entry->node); + entry->entry_tid = ceph_decode_64(p); + entry->tag_tid = ceph_decode_64(p); + entry->data = ceph_extract_encoded_string(p, end, &entry->data_len, GFP_NOIO); + if (!entry->data) + goto free_entry; + + crc = crc32c(0, start, *p - start); + + crc_encoded = ceph_decode_32(p); + + if (crc != crc_encoded) + goto free_data; + return entry; + +free_data: + kfree(entry->data); +free_entry: + kfree(entry); +err: + return NULL; +} + +// TODO refactor this function to make it more readable and more effective on memory usage. +static int fetch(struct ceph_journaler *journaler, uint64_t object_num) +{ + struct ceph_object_id object_oid; + int ret = 0; + void *buf = NULL, *read_buf = NULL, *buf_p = NULL; + void *end = NULL; + uint64_t read_len = 2 << journaler->order; + uint32_t read_off = 0; + uint64_t buf_len = read_len; + struct list_head entry_list; + bool position_found = false; + struct ceph_journaler_object_pos *pos; + struct object_replayer *obj_replayer = NULL; + + obj_replayer = &journaler->obj_replayers[object_num % journaler->splay_width]; + obj_replayer->object_num = object_num; + list_for_each_entry(pos, &journaler->client->object_positions, node) { + if (pos->object_num == object_num) { + position_found = true; + break; + } + } + + if (position_found) + obj_replayer->pos = pos; + + INIT_LIST_HEAD(&entry_list); + ceph_oid_init(&object_oid); + ret = ceph_oid_aprintf(&object_oid, GFP_KERNEL, "%s%llu", + journaler->object_oid_prefix, object_num); + if (ret) { + pr_err("failed to initialize object_id : %d", ret); + return ret; + } + + buf = vmalloc(buf_len); + if (!buf) { + pr_err("failed to vmalloc buf: %llu", buf_len); + ret = -ENOMEM; + goto err_free_object_oid; + } + read_buf = buf; + buf_p = buf; + +refetch: + ret = ceph_journaler_obj_read_sync(journaler, &object_oid, + &journaler->data_oloc, read_buf, + read_off, read_len); + if (ret == -ENOENT) { + pr_err("no such object, %s: %d", object_oid.name, ret); + goto err_free_buf; + } else if (ret < 0) { + pr_err("failed to read: %d", ret); + goto err_free_buf; + } else if (ret == 0) { + pr_err("no data: %d", ret); + goto err_free_buf; + } + read_off = read_off + ret; + + end = read_buf + ret; + while (buf < end) { + uint32_t bytes_needed = 0; + struct ceph_journaler_entry *entry = NULL; + + if (!entry_is_readable(journaler, buf, end, &bytes_needed)) { + uint64_t remain = end - buf; + pr_err("not readable"); + if (bytes_needed != 0) { + void *new_buf = vmalloc(read_len + remain); + if (!new_buf) { + pr_err("failed to alloc new buf"); + goto err_free_buf; + } + memcpy(new_buf, buf, remain); + vfree(buf_p); + buf_p = new_buf; + buf = new_buf; + read_buf = buf + remain; + goto refetch; + } else { + pr_err("entry corruption"); + goto err_free_buf; + } + } + entry = journaler_entry_decode(&buf, end); + if (!entry) + goto err_free_buf; + + list_add_tail(&entry->node, &obj_replayer->entry_list); + } + ret = 0; + +err_free_buf: + vfree(buf_p); +err_free_object_oid: + ceph_oid_destroy(&object_oid); + return ret; +} + +static int add_commit_entry(struct ceph_journaler *journaler, uint64_t commit_tid, + uint64_t object_num, uint64_t tag_tid, uint64_t entry_tid) +{ + struct commit_entry *entry = NULL; + int ret = 0; + + entry = kzalloc(sizeof(*entry), GFP_KERNEL); + if (entry == NULL) { + ret = -ENOMEM; + goto out; + } + RB_CLEAR_NODE(&entry->r_node); + + entry->commit_tid = commit_tid; + entry->object_num = object_num; + entry->tag_tid = tag_tid; + entry->entry_tid = entry_tid; + + spin_lock(&journaler->commit_lock); + insert_commit_entry(&journaler->commit_entries, entry); + spin_unlock(&journaler->commit_lock); + +out: + return ret; +} + +static uint64_t allocate_commit_tid(struct ceph_journaler *journaler) +{ + return ++journaler->commit_tid; +} + +static int get_first_entry(struct ceph_journaler *journaler, + struct ceph_journaler_entry **entry, + uint64_t *commit_tid) +{ + struct object_replayer *obj_replayer = NULL; + struct ceph_journaler_entry *tmp_entry = NULL; + uint64_t last_entry_tid = 0; + int ret = 0; + + obj_replayer = &journaler->obj_replayers[journaler->splay_offset]; + + if (list_empty(&obj_replayer->entry_list)) { + return -ENOENT; + } + + tmp_entry = list_first_entry(&obj_replayer->entry_list, + struct ceph_journaler_entry, node); + list_del(&tmp_entry->node); + + ret = get_last_entry_tid(journaler, tmp_entry->tag_tid, &last_entry_tid); + if (ret && tmp_entry->entry_tid != last_entry_tid + 1) { + pr_err("missing prior journal entry, last_entry_tid: %llu", + last_entry_tid); + ret = -ENOMSG; + goto free_entry; + } + + journaler->active_tag_tid = tmp_entry->tag_tid; + journaler->splay_offset = (journaler->splay_offset + 1) % journaler->splay_width; + + if (list_empty(&obj_replayer->entry_list)) { + ret = fetch(journaler, obj_replayer->object_num + journaler->splay_width); + if (ret && ret != -ENOENT) { + goto free_entry; + } + } + + ret = reserve_entry_tid(journaler, tmp_entry->tag_tid, tmp_entry->entry_tid); + if (ret) + goto free_entry; + + *commit_tid = allocate_commit_tid(journaler); + ret = add_commit_entry(journaler, *commit_tid, obj_replayer->object_num, + tmp_entry->tag_tid, tmp_entry->entry_tid); + if (ret) + goto free_entry; + + *entry = tmp_entry; + return 0; + +free_entry: + kfree(tmp_entry); + return ret; +} + +static void prune_tag(struct ceph_journaler *journaler) +{ + struct ceph_journaler_entry *entry, *next; + struct object_replayer *obj_replayer = NULL; + int i = 0; + + for (i = 0; i < journaler->splay_width; i++) { + obj_replayer = &journaler->obj_replayers[i]; + list_for_each_entry_safe(entry, next, + &obj_replayer->entry_list, node) { + if (entry->tag_tid == journaler->active_tag_tid) { + list_del(&entry->node); + kfree(entry); + } + } + } +} + +static int process_replay(struct ceph_journaler *journaler) +{ + int r = 0; + struct ceph_journaler_entry *entry = NULL; + uint64_t commit_tid; + +next: + r = get_first_entry(journaler, &entry, &commit_tid); + if (r) { + if (r == -ENOENT) { + prune_tag(journaler); + r = 0; + } else { + kfree(entry); + } + return r; + } + + r = playback_entry(journaler, entry, commit_tid); + if (r) { + kfree(entry); + return r; + } + kfree(entry); + + goto next; +} + +static int preprocess_replay(struct ceph_journaler *journaler) +{ + struct ceph_journaler_entry *entry, *next; + bool found_commit = false; + struct object_replayer *obj_replayer = NULL; + int i = 0; + int ret = 0; + + for (i = 0; i < journaler->splay_width; i++) { + obj_replayer = &journaler->obj_replayers[i]; + + if (!obj_replayer->pos) + continue; + + found_commit = false; + list_for_each_entry_safe(entry, next, + &obj_replayer->entry_list, node) { + if (entry->tag_tid == obj_replayer->pos->tag_tid && + entry->entry_tid == obj_replayer->pos->entry_tid) { + found_commit = true; + } else if (found_commit) { + break; + } + + ret = reserve_entry_tid(journaler, entry->tag_tid, entry->entry_tid); + if (ret) + return ret; + list_del(&entry->node); + kfree(entry); + } + } + return 0; +} + +int ceph_journaler_start_replay(struct ceph_journaler *journaler) +{ + struct ceph_journaler_object_pos *active_pos = NULL; + struct ceph_journaler_client *client = NULL; + uint64_t *fetch_objects = NULL; + uint64_t object_num; + int i = 0; + int ret = 0; + + fetch_objects = kzalloc(sizeof(uint64_t) * journaler->splay_width, GFP_KERNEL); + if (!fetch_objects) { + return -ENOMEM; + } + + spin_lock(&journaler->meta_lock); + client = journaler->client; + if (!list_empty(&journaler->client->object_positions)) { + active_pos = list_first_entry(&journaler->client->object_positions, + struct ceph_journaler_object_pos, node); + + journaler->splay_offset = (active_pos->object_num + 1) % journaler->splay_width; + + list_for_each_entry(active_pos, &client->object_positions, node) { + fetch_objects[active_pos->object_num % + journaler->splay_width] = active_pos->object_num; + } + } + spin_unlock(&journaler->meta_lock); + + for (i = 0; i < journaler->splay_width; i++) { + if (fetch_objects[i] == 0) { + object_num = i; + } else { + object_num = fetch_objects[i]; + } + ret = fetch(journaler, object_num); + if (ret && ret != -ENOENT) + goto out; + } + ret = preprocess_replay(journaler); + if (ret) + goto out; + + ret = process_replay(journaler); +out: + for (i = 0; i < journaler->splay_width; i++) { + struct object_replayer *obj_replayer = &journaler->obj_replayers[i]; + struct ceph_journaler_entry *entry = NULL, *next_entry = NULL; + + spin_lock(&obj_replayer->lock); + list_for_each_entry_safe(entry, next_entry, &obj_replayer->entry_list, node) { + list_del(&entry->node); + kfree(entry); + } + spin_unlock(&obj_replayer->lock); + } + kfree(fetch_objects); + return ret; +} +EXPORT_SYMBOL(ceph_journaler_start_replay); From patchwork Mon Dec 3 12:50:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709511 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A21CD13BF for ; Mon, 3 Dec 2018 12:57:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8F9FF2B408 for ; Mon, 3 Dec 2018 12:57:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 80EDE2B54C; Mon, 3 Dec 2018 12:57:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2806B2B408 for ; Mon, 3 Dec 2018 12:56:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726597AbeLCM5x (ORCPT ); Mon, 3 Dec 2018 07:57:53 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22203 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726563AbeLCM5x (ORCPT ); Mon, 3 Dec 2018 07:57:53 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S8; Mon, 03 Dec 2018 20:50:41 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 06/11] libceph: journaling: introduce api for journal appending Date: Mon, 3 Dec 2018 07:50:30 -0500 Message-Id: <1543841435-13652-7-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S8 X-Coremail-Antispam: 1Uf129KBjvAXoWftFW8KrW8AryDuw4UZF4DCFg_yoW8trWxuo W2gr4DuFn5Ga4UuFW8Kr18Ja4fXa48Ja4rArWYgF4Y9anrAry8Z3y7Gr15tFyY9w45CrsF qw1xJwn3JF4kJ3WUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfULsYwUUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbihQESelsflXmXgAAAsT Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This commit introduce 3 APIs for journal recording: (1) ceph_journaler_allocate_tag() This api allocate a new tag for user to get a unified tag_tid. Then each event appended by this user will be tagged by this tag_tid. (2) ceph_journaler_append() This api allow user to append event to journal objects. (3) ceph_journaler_client_committed() This api will notify journaling that a event is already committed, you can remove it from journal if there is no other client refre to it. Signed-off-by: Dongsheng Yang --- include/linux/ceph/journaler.h | 12 + net/ceph/journaler.c | 678 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 690 insertions(+) diff --git a/include/linux/ceph/journaler.h b/include/linux/ceph/journaler.h index acc1c6f..a8c1838 100644 --- a/include/linux/ceph/journaler.h +++ b/include/linux/ceph/journaler.h @@ -132,6 +132,7 @@ struct ceph_journaler { struct workqueue_struct *task_wq; struct workqueue_struct *finish_wq; + struct workqueue_struct *notify_wq; struct work_struct notify_update_work; struct work_struct commit_work; struct work_struct finish_work; @@ -161,4 +162,15 @@ int ceph_journaler_get_cached_client(struct ceph_journaler *journaler, char *cli struct ceph_journaler_client **client_result); // replaying int ceph_journaler_start_replay(struct ceph_journaler *journaler); + +// recording +int ceph_journaler_append(struct ceph_journaler *journaler, + uint64_t tag_tid, uint64_t *commit_tid, + struct ceph_journaler_ctx *ctx); +void ceph_journaler_client_committed(struct ceph_journaler *journaler, + uint64_t commit_tid); +int ceph_journaler_allocate_tag(struct ceph_journaler *journaler, + uint64_t tag_class, void *buf, + uint32_t buf_len, + struct ceph_journaler_tag *tag); #endif diff --git a/net/ceph/journaler.c b/net/ceph/journaler.c index 3b73725..e0dce2a 100644 --- a/net/ceph/journaler.c +++ b/net/ceph/journaler.c @@ -28,6 +28,11 @@ static char *object_oid_prefix(int pool_id, const char *journal_id) } static void watch_cb_func(struct work_struct *work); +static void journaler_flush(struct work_struct *work); +static void journaler_finish(struct work_struct *work); +static void journaler_client_commit(struct work_struct *work); +static void journaler_notify_update(struct work_struct *work); +static void journaler_overflow(struct work_struct *work); struct ceph_journaler *ceph_journaler_create(struct ceph_osd_client *osdc, struct ceph_object_locator *oloc, @@ -98,6 +103,11 @@ struct ceph_journaler *ceph_journaler_create(struct ceph_osd_client *osdc, spin_lock_init(&journaler->finish_lock); INIT_WORK(&journaler->watch_cb_work, watch_cb_func); + INIT_WORK(&journaler->flush_work, journaler_flush); + INIT_WORK(&journaler->finish_work, journaler_finish); + INIT_WORK(&journaler->commit_work, journaler_client_commit); + INIT_WORK(&journaler->notify_update_work, journaler_notify_update); + INIT_WORK(&journaler->overflow_work, journaler_overflow); return journaler; @@ -1046,3 +1056,671 @@ int ceph_journaler_start_replay(struct ceph_journaler *journaler) return ret; } EXPORT_SYMBOL(ceph_journaler_start_replay); + +// recording +// TODO use kmem_cache for it. +struct journaler_write_ctx { + struct list_head node; + + struct ceph_journaler *journaler; + struct ceph_journaler_future *future; + struct ceph_journaler_ctx *journaler_ctx; + + uint64_t splay_offset; + uint64_t object_num; + + struct page *req_page; + struct ceph_bio_iter *bio_iter; +}; + +static int allocate_entry_tid(struct ceph_journaler *journaler, + uint64_t tag_tid, uint64_t *entry_tid) +{ + struct entry_tid *pos = NULL; + + spin_lock(&journaler->entry_tid_lock); + list_for_each_entry(pos, &journaler->entry_tids, node) { + if (pos->tag_tid == tag_tid) { + *entry_tid = pos->entry_tid++; + spin_unlock(&journaler->entry_tid_lock); + return 0; + } + } + pos = kzalloc(sizeof(struct entry_tid), GFP_KERNEL); + WARN_ON(pos == NULL); + if (!pos) { + spin_unlock(&journaler->entry_tid_lock); + pr_err("failed to allocate new entry."); + return -ENOMEM; + } + + pos->tag_tid = tag_tid; + pos->entry_tid = 0; + INIT_LIST_HEAD(&pos->node); + + list_add_tail(&pos->node, &journaler->entry_tids); + *entry_tid = pos->entry_tid++; + spin_unlock(&journaler->entry_tid_lock); + + return 0; +} + +static uint64_t get_object(struct ceph_journaler *journaler, uint64_t splay_offset) +{ + return splay_offset + (journaler->splay_width * journaler->active_set); +} + +static struct ceph_journaler_future *create_future(uint64_t tag_tid, + uint64_t entry_tid, + uint64_t commit_tid) +{ + struct ceph_journaler_future *future = NULL; + + future = kzalloc(sizeof(struct ceph_journaler_future), GFP_KERNEL); + if (!future) + return NULL; + + future->tag_tid = tag_tid; + future->entry_tid = entry_tid; + future->commit_tid = commit_tid; + + future->safe = false; + future->consistent = false; + + future->ctx = NULL; + future->wait = NULL; + + return future; +} + +static void set_prev_future(struct ceph_journaler *journaler, + struct ceph_journaler_future *future) +{ + if (journaler->prev_future == NULL) { + future->consistent = true; + } else if (journaler->prev_future->consistent && + journaler->prev_future->safe) { + future->consistent = true; + } else { + journaler->prev_future->wait = future; + } + journaler->prev_future = future; +} + +static struct ceph_journaler_entry *create_entry(uint64_t tag_tid, + uint64_t entry_tid, + struct ceph_bio_iter *bio_iter) +{ + struct ceph_journaler_entry *entry = NULL; + + entry = kzalloc(sizeof(struct ceph_journaler_entry), GFP_KERNEL); + if (!entry) + return NULL; + entry->tag_tid = tag_tid; + entry->entry_tid = entry_tid; + entry->bio_iter = bio_iter; + entry->data_len = bio_iter->bio_len + + bio_iter->prefix_len + bio_iter->suffix_len; + + return entry; +} + +static void journaler_entry_encode_prefix(struct ceph_journaler_entry *entry, + void **p, void *end) +{ + ceph_encode_64(p, PREAMBLE); + ceph_encode_8(p, (uint8_t)1); + ceph_encode_64(p, entry->entry_tid); + ceph_encode_64(p, entry->tag_tid); + + ceph_encode_32(p, entry->data_len); +} + +static uint32_t crc_bio(uint32_t crc, struct bio *bio) +{ + struct bio_vec bv; + struct bvec_iter iter; + char *buf = NULL; + u64 offset = 0; + +next: + bio_for_each_segment(bv, bio, iter) { + buf = page_address(bv.bv_page) + bv.bv_offset; + crc = crc32c(crc, buf, bv.bv_len); + offset += bv.bv_len; + } + + if (bio->bi_next) { + bio = bio->bi_next; + goto next; + } + + return crc; +} + +static void journaler_finish(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, struct ceph_journaler, + finish_work); + struct ceph_journaler_ctx *ctx_pos, *next; + + spin_lock(&journaler->finish_lock); + list_for_each_entry_safe(ctx_pos, next, &journaler->ctx_list, node) { + list_del(&ctx_pos->node); + ctx_pos->callback(ctx_pos); + } + spin_unlock(&journaler->finish_lock); +} + +static void future_consistent(struct ceph_journaler *journaler, + struct ceph_journaler_future *future); +static void future_finish(struct ceph_journaler *journaler, + struct ceph_journaler_future *future) { + struct ceph_journaler_ctx *journaler_ctx = future->ctx; + + if (future->safe && future->consistent) { + spin_lock(&journaler->finish_lock); + list_add_tail(&journaler_ctx->node, &journaler->ctx_list); + spin_unlock(&journaler->finish_lock); + + queue_work(journaler->finish_wq, &journaler->finish_work); + + if (future->wait) + future_consistent(journaler, future->wait); + } +} + +static void future_consistent(struct ceph_journaler *journaler, + struct ceph_journaler_future *future) { + future->consistent = true; + future_finish(journaler, future); +} + +static void future_safe(struct ceph_journaler *journaler, + struct ceph_journaler_future *future) { + future->safe = true; + future_finish(journaler, future); +} + +static void journaler_notify_update(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, + struct ceph_journaler, + notify_update_work); + int ret = 0; + + ret = ceph_osdc_notify(journaler->osdc, &journaler->header_oid, + &journaler->header_oloc, NULL, 0, + 5000, NULL, NULL); + if (ret) + pr_err("notify_update failed: %d", ret); +} + +static bool advance_object_set(struct ceph_journaler *journaler) +{ + int ret = 0; + int i = 0; + struct object_recorder *obj_recorder; + uint64_t active_set = 0; + + spin_lock(&journaler->advancing_lock); + if (journaler->advancing) { + spin_unlock(&journaler->advancing_lock); + return false; + } + + // make sure all inflight appending finish + for (i = 0; i < journaler->splay_width; i++) { + obj_recorder = &journaler->obj_recorders[i]; + spin_lock(&obj_recorder->lock); + if (obj_recorder->inflight_append) { + spin_unlock(&obj_recorder->lock); + spin_unlock(&journaler->advancing_lock); + return false; + } + spin_unlock(&obj_recorder->lock); + } + + journaler->advancing = true; + + active_set = journaler->active_set + 1; + spin_unlock(&journaler->advancing_lock); + + ret = ceph_cls_journaler_set_active_set(journaler->osdc, + &journaler->header_oid, &journaler->header_oloc, + active_set); + if (ret) { + pr_err("error in set active_set: %d", ret); + } + + queue_work(journaler->task_wq, &journaler->notify_update_work); + + return true; +} + +static void journaler_overflow(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, + struct ceph_journaler, + overflow_work); + if (advance_object_set(journaler)) { + queue_work(journaler->task_wq, &journaler->flush_work); + } +} + +static void journaler_write_callback(struct ceph_osd_request *osd_req) +{ + struct journaler_write_ctx *ctx = osd_req->r_priv; + struct ceph_journaler *journaler = ctx->journaler; + struct ceph_journaler_ctx *journaler_ctx = ctx->journaler_ctx; + struct ceph_journaler_future *future = ctx->future; + int ret = osd_req->r_result; + struct object_recorder *obj_recorder = &journaler->obj_recorders[ctx->splay_offset]; + + __free_page(ctx->req_page); + ceph_osdc_put_request(osd_req); + + if (ret == -EOVERFLOW) { + spin_lock(&obj_recorder->lock); + if (!obj_recorder->overflowed) + obj_recorder->overflowed = true; + list_add_tail(&ctx->node, &obj_recorder->overflow_list); + if (--obj_recorder->inflight_append == 0) + queue_work(journaler->task_wq, &journaler->overflow_work); + spin_unlock(&obj_recorder->lock); + return; + } + + spin_lock(&obj_recorder->lock); + --obj_recorder->inflight_append; + spin_unlock(&obj_recorder->lock); + + ret = add_commit_entry(journaler, ctx->future->commit_tid, ctx->object_num, + ctx->future->tag_tid, ctx->future->entry_tid); + if (ret) { + pr_err("failed to add_commit_entry"); + } + + journaler_ctx->result = ret; + future_safe(journaler, future); + kfree(ctx); + + if (journaler_ctx->bio_iter->prefix_page) + __free_page(journaler_ctx->bio_iter->prefix_page); + if (journaler_ctx->bio_iter->suffix_page) + __free_page(journaler_ctx->bio_iter->suffix_page); +} + +static int ceph_journaler_obj_write(struct ceph_journaler *journaler, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc, + struct journaler_write_ctx *ctx) + +{ + struct ceph_osd_client *osdc = journaler->osdc; + struct ceph_osd_request *req; + void *p; + int ret; + + req = ceph_osdc_alloc_request(osdc, NULL, 2, false, GFP_NOIO); + if (!req) + return -ENOMEM; + + ceph_oid_copy(&req->r_base_oid, oid); + ceph_oloc_copy(&req->r_base_oloc, oloc); + req->r_flags = CEPH_OSD_FLAG_WRITE; + req->r_callback = journaler_write_callback; + req->r_priv = ctx; + + // guard_append + ctx->req_page = alloc_page(GFP_NOIO); + if (!ctx->req_page) { + ret = -ENOMEM; + goto out_req; + } + + p = page_address(ctx->req_page); + ceph_encode_64(&p, 1 << journaler->order); + + ret = osd_req_op_cls_init(req, 0, "journal", "guard_append"); + if (ret) + goto out_free_page; + + osd_req_op_cls_request_data_pages(req, 0, &ctx->req_page, 8, 0, false, false); + + // append_data + osd_req_op_extent_init(req, 1, CEPH_OSD_OP_APPEND, 0, + ctx->bio_iter->prefix_len + ctx->bio_iter->bio_len + ctx->bio_iter->suffix_len, 0, 0); + osd_req_op_extent_osd_data_bio(req, 1, ctx->bio_iter, + ctx->bio_iter->prefix_len + ctx->bio_iter->bio_len + ctx->bio_iter->suffix_len); + ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); + if (ret) + goto out_free_page; + + ceph_osdc_start_request(osdc, req, false); + return 0; + +out_free_page: + __free_page(ctx->req_page); +out_req: + ceph_osdc_put_request(req); + return ret; +} + +static int send_append_request(struct ceph_journaler *journaler, + uint64_t object_num, + struct journaler_write_ctx *ctx) +{ + struct ceph_object_id object_oid; + int ret = 0; + + ceph_oid_init(&object_oid); + ret = ceph_oid_aprintf(&object_oid, GFP_KERNEL, "%s%llu", + journaler->object_oid_prefix, object_num); + if (ret) { + pr_err("failed to initialize object id: %d", ret); + goto out; + } + + ret = ceph_journaler_obj_write(journaler, &object_oid, + &journaler->data_oloc, ctx); +out: + ceph_oid_destroy(&object_oid); + return ret; +} + +static void journaler_flush(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, + struct ceph_journaler, + flush_work); + int i = 0; + int ret = 0; + struct object_recorder *obj_recorder; + struct journaler_write_ctx *ctx; + int req_num = 0; + LIST_HEAD(tmp); + + if (journaler->advancing) { + return; + } + + for (i = 0; i < journaler->splay_width; i++) { + req_num = 0; + INIT_LIST_HEAD(&tmp); + obj_recorder = &journaler->obj_recorders[i]; + spin_lock(&obj_recorder->lock); + list_splice_tail_init(&obj_recorder->overflow_list, &tmp); + list_splice_tail_init(&obj_recorder->append_list, &tmp); + spin_unlock(&obj_recorder->lock); + + list_for_each_entry(ctx, &tmp, node) { + ctx->object_num = get_object(journaler, obj_recorder->splay_offset); + ret = send_append_request(journaler, ctx->object_num, ctx); + if (ret) { + // TODO + pr_err("failed to send append request: %d", ret); + } + req_num++; + } + + spin_lock(&obj_recorder->lock); + obj_recorder->inflight_append += req_num; + spin_unlock(&obj_recorder->lock); + } +} + +static int ceph_journaler_object_append(struct ceph_journaler *journaler, + uint64_t splay_offset, + struct ceph_journaler_future *future, + struct ceph_journaler_entry *entry, + struct ceph_journaler_ctx *journaler_ctx) +{ + void *buf = NULL; + void *end = NULL; + int ret = 0; + uint32_t crc = 0; + struct ceph_bio_iter *bio_iter = journaler_ctx->bio_iter; + struct journaler_write_ctx *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + struct object_recorder *obj_recorder; + + if (!ctx) { + return -ENOMEM; + } + + // PEAMBLE(8) + version(1) + entry_tid(8) + tag_tid(8) + string_len(4) = 29 + bio_iter->prefix_offset = bio_iter->prefix_offset - 29; + bio_iter->prefix_len = bio_iter->prefix_len + 29; + buf = page_address(bio_iter->prefix_page) + bio_iter->prefix_offset; + end = buf + 29; + + ctx->journaler = journaler; + ctx->future = future; + ctx->journaler_ctx = journaler_ctx; + ctx->splay_offset = splay_offset; + INIT_LIST_HEAD(&ctx->node); + + journaler_entry_encode_prefix(entry, &buf, end); + + // size of crc is 4 + bio_iter->suffix_page = alloc_page(GFP_KERNEL); + bio_iter->suffix_offset = 0; + bio_iter->suffix_len = 4; + + buf = page_address(bio_iter->suffix_page); + end = buf + 4; + + crc = crc32c(crc, page_address(bio_iter->prefix_page) + bio_iter->prefix_offset, + bio_iter->prefix_len); + if (bio_iter->bio_len) + crc = crc_bio(crc, bio_iter->bio); + + ceph_encode_32(&buf, crc); + ctx->bio_iter = bio_iter; + + obj_recorder = &journaler->obj_recorders[ctx->splay_offset]; + spin_lock(&obj_recorder->lock); + list_add_tail(&ctx->node, &obj_recorder->append_list); + queue_work(journaler->task_wq, &journaler->flush_work); + spin_unlock(&obj_recorder->lock); + + return ret; +} + +int ceph_journaler_append(struct ceph_journaler *journaler, uint64_t tag_tid, + uint64_t *commit_tid, struct ceph_journaler_ctx *ctx) +{ + uint8_t splay_width; + uint8_t splay_offset; + uint64_t object_num; + uint64_t entry_tid; + struct ceph_journaler_future *future; + struct ceph_journaler_entry *entry; + struct object_recorder *obj_recorder; + int ret = 0; + + spin_lock(&journaler->meta_lock); + ret = allocate_entry_tid(journaler, tag_tid, &entry_tid); + if (ret) { + goto unlock; + } + + splay_width = journaler->splay_width; + splay_offset = entry_tid % splay_width; + obj_recorder = &journaler->obj_recorders[splay_width]; + + object_num = get_object(journaler, splay_offset); + *commit_tid = allocate_commit_tid(journaler); + + future = create_future(tag_tid, entry_tid, *commit_tid); + if (!future) { + ret = -ENOMEM; + goto unlock; + } + + future->ctx = ctx; + set_prev_future(journaler, future); + + entry = create_entry(tag_tid, entry_tid, ctx->bio_iter); + if (!entry) { + ret = -ENOMEM; + goto unlock; + } + spin_unlock(&journaler->meta_lock); + + ret = ceph_journaler_object_append(journaler, splay_offset, future, entry, ctx); + return ret; + +unlock: + spin_unlock(&journaler->meta_lock); + return ret; +} +EXPORT_SYMBOL(ceph_journaler_append); + +static void journaler_client_commit(struct work_struct *work) +{ + struct ceph_journaler *journaler = container_of(work, struct ceph_journaler, + commit_work); + + struct list_head object_positions; + struct ceph_journaler_object_pos *pos = NULL, *next = NULL; + int ret = 0; + + INIT_LIST_HEAD(&object_positions); + spin_lock(&journaler->commit_lock); + list_for_each_entry_safe(pos, next, &journaler->object_positions_pending, + node) { + struct ceph_journaler_object_pos *new_pos = NULL; + + ret = copy_object_pos(pos, &new_pos); + list_add_tail(&new_pos->node, &object_positions); + } + spin_unlock(&journaler->commit_lock); + + ret = ceph_cls_journaler_client_committed(journaler->osdc, + &journaler->header_oid, &journaler->header_oloc, + journaler->client, &object_positions); + + if (ret) { + pr_err("error in client committed: %d", ret); + } + + list_for_each_entry_safe(pos, next, &object_positions, node) { + list_del(&pos->node); + kfree(pos); + } + + queue_work(journaler->notify_wq, &journaler->notify_update_work); + return; +} + +// hold journaler->commit_lock +static int add_object_position(struct commit_entry *entry, + struct list_head *object_positions, + uint64_t splay_width) +{ + struct ceph_journaler_object_pos *position = NULL; + uint8_t splay_offset = entry->object_num % splay_width; + bool found = false; + int ret = 0; + + list_for_each_entry(position, object_positions, node) { + if (splay_offset == position->object_num % splay_width) { + found = true; + break; + } + } + + if (!found) { + position = kzalloc(sizeof(*position), GFP_KERNEL); + + if (!position) { + pr_err("failed to allocate position"); + return -ENOMEM; + } + list_add(&position->node, object_positions); + } else { + list_move(&position->node, object_positions); + } + + position->object_num = entry->object_num; + position->tag_tid = entry->tag_tid; + position->entry_tid = entry->entry_tid; + + return ret; +} + +void ceph_journaler_client_committed(struct ceph_journaler *journaler, uint64_t commit_tid) +{ + struct commit_entry *entry = NULL; + bool update_client_commit = true; + struct rb_node *n; + LIST_HEAD(object_positions); + + spin_lock(&journaler->commit_lock); + for (n = rb_first(&journaler->commit_entries); n; n = rb_next(n)) { + entry = rb_entry(n, struct commit_entry, r_node); + if (entry->commit_tid == commit_tid) { + entry->committed = true; + break; + } + if (entry->committed == false) + update_client_commit = false; + } + + if (update_client_commit) { + for (n = rb_first(&journaler->commit_entries); n;) { + entry = rb_entry(n, struct commit_entry, r_node); + n = rb_next(n); + + if (entry->commit_tid > commit_tid) + break; + add_object_position(entry, + &journaler->object_positions_pending, + journaler->splay_width); + erase_commit_entry(&journaler->commit_entries, entry); + kfree(entry); + } + } + spin_unlock(&journaler->commit_lock); + + if (update_client_commit) { + queue_work(journaler->task_wq, &journaler->commit_work); + } +} +EXPORT_SYMBOL(ceph_journaler_client_committed); + +int ceph_journaler_allocate_tag(struct ceph_journaler *journaler, + uint64_t tag_class, void *buf, + uint32_t buf_len, + struct ceph_journaler_tag *tag) +{ + uint64_t tag_tid = 0; + int ret = 0; + + ret = ceph_cls_journaler_get_next_tag_tid(journaler->osdc, + &journaler->header_oid, + &journaler->header_oloc, + &tag_tid); + if (ret) + goto out; + + ret = ceph_cls_journaler_tag_create(journaler->osdc, + &journaler->header_oid, + &journaler->header_oloc, + tag_tid, tag_class, + buf, buf_len); + if (ret) + goto out; + + ret = ceph_cls_journaler_get_tag(journaler->osdc, + &journaler->header_oid, + &journaler->header_oloc, + tag_tid, tag); + if (ret) + goto out; + +out: + return ret; +} +EXPORT_SYMBOL(ceph_journaler_allocate_tag); From patchwork Mon Dec 3 12:50:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709517 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F3FEE16B1 for ; Mon, 3 Dec 2018 12:59:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4D0C2AB1A for ; Mon, 3 Dec 2018 12:59:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D86952ABBB; Mon, 3 Dec 2018 12:59:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6AFE02AB1A for ; Mon, 3 Dec 2018 12:59:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725914AbeLCNAb (ORCPT ); Mon, 3 Dec 2018 08:00:31 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22216 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726420AbeLCNAb (ORCPT ); Mon, 3 Dec 2018 08:00:31 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S9; Mon, 03 Dec 2018 20:50:42 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 07/11] libceph: journaling: trim object set when we found there is no client refer it Date: Mon, 3 Dec 2018 07:50:31 -0500 Message-Id: <1543841435-13652-8-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S9 X-Coremail-Antispam: 1Uf129KBjvJXoWxAFW8JrWktw18WF4DAr1UJrb_yoWrZFWfpr sxXr4SyrW0vF1furs7JrsYqFZ09rW0vFW7GrnIkF9ay3W7XrZIgF18JFyqqry7tr13G3Z8 tF4UJa15Ww42qFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbK3ktUUUUU= X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbigAISelrpN9XK6wAAsa Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we found there is no client refre to the object set, we can remove the objects. Signed-off-by: Dongsheng Yang --- net/ceph/journaler.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/net/ceph/journaler.c b/net/ceph/journaler.c index e0dce2a..1a3651e 100644 --- a/net/ceph/journaler.c +++ b/net/ceph/journaler.c @@ -142,6 +142,10 @@ void ceph_journaler_destroy(struct ceph_journaler *journaler) } EXPORT_SYMBOL(ceph_journaler_destroy); +static int remove_set(struct ceph_journaler *journaler, uint64_t object_set); +static int set_minimum_set(struct ceph_journaler* journaler, + uint64_t minimum_set); + static int refresh(struct ceph_journaler *journaler, bool init) { int ret = 0; @@ -207,6 +211,34 @@ static int refresh(struct ceph_journaler *journaler, bool init) } spin_unlock(&journaler->meta_lock); + if (need_advance) { + spin_lock(&journaler->advancing_lock); + journaler->advancing = false; + journaler->active_set = active_set; + spin_unlock(&journaler->advancing_lock); + + queue_work(journaler->task_wq, &journaler->flush_work); + } + + // remove set if necessary + if (minimum_commit_set > minimum_set) { + uint64_t trim_set = minimum_set; + while (trim_set < minimum_commit_set) { + ret = remove_set(journaler, trim_set); + if (ret < 0 && ret != -ENOENT) { + pr_err("failed to trim object_set: %llu", trim_set); + return ret; + } + trim_set++; + } + + ret = set_minimum_set(journaler, minimum_commit_set); + if (ret < 0) { + pr_err("failed to set minimum set to %llu", minimum_commit_set); + return ret; + } + } + return 0; } @@ -1293,7 +1325,7 @@ static bool advance_object_set(struct ceph_journaler *journaler) pr_err("error in set active_set: %d", ret); } - queue_work(journaler->task_wq, &journaler->notify_update_work); + queue_work(journaler->notify_wq, &journaler->notify_update_work); return true; } @@ -1724,3 +1756,88 @@ int ceph_journaler_allocate_tag(struct ceph_journaler *journaler, return ret; } EXPORT_SYMBOL(ceph_journaler_allocate_tag); + +// trimming +static int ceph_journaler_obj_remove_sync(struct ceph_journaler *journaler, + struct ceph_object_id *oid, + struct ceph_object_locator *oloc) + +{ + struct ceph_osd_client *osdc = journaler->osdc; + struct ceph_osd_request *req; + int ret; + + req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); + if (!req) + return -ENOMEM; + + ceph_oid_copy(&req->r_base_oid, oid); + ceph_oloc_copy(&req->r_base_oloc, oloc); + req->r_flags = CEPH_OSD_FLAG_WRITE; + + osd_req_op_init(req, 0, CEPH_OSD_OP_DELETE, 0); + + ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); + if (ret) + goto out_req; + + ceph_osdc_start_request(osdc, req, false); + ret = ceph_osdc_wait_request(osdc, req); + +out_req: + ceph_osdc_put_request(req); + return ret; +} + +static int remove_set(struct ceph_journaler *journaler, uint64_t object_set) +{ + uint64_t object_num = 0; + int splay_offset = 0; + struct ceph_object_id object_oid; + int ret = 0; + + ceph_oid_init(&object_oid); + for (splay_offset = 0; splay_offset < journaler->splay_width; splay_offset++) { + object_num = splay_offset + (object_set * journaler->splay_width); + if (!ceph_oid_empty(&object_oid)) { + ceph_oid_destroy(&object_oid); + ceph_oid_init(&object_oid); + } + ret = ceph_oid_aprintf(&object_oid, GFP_KERNEL, "%s%llu", + journaler->object_oid_prefix, object_num); + if (ret) { + pr_err("aprintf error : %d", ret); + goto out; + } + ret = ceph_journaler_obj_remove_sync(journaler, &object_oid, + &journaler->data_oloc); + if (ret < 0 && ret != -ENOENT) { + pr_err("%s: failed to remove object: %llu", + __func__, object_num); + goto out; + } + } + ret = 0; +out: + ceph_oid_destroy(&object_oid); + return ret; +} + +static int set_minimum_set(struct ceph_journaler* journaler, + uint64_t minimum_set) +{ + int ret = 0; + + ret = ceph_cls_journaler_set_minimum_set(journaler->osdc, + &journaler->header_oid, + &journaler->header_oloc, + minimum_set); + if (ret < 0) { + pr_err("%s: failed to set_minimum_set: %d", __func__, ret); + return ret; + } + + queue_work(journaler->notify_wq, &journaler->notify_update_work); + + return ret; +} From patchwork Mon Dec 3 12:50:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709497 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E476613BF for ; Mon, 3 Dec 2018 12:56:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5B032B408 for ; Mon, 3 Dec 2018 12:56:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C95722B4AF; Mon, 3 Dec 2018 12:56:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 69AE22B408 for ; Mon, 3 Dec 2018 12:56:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726557AbeLCM5F (ORCPT ); Mon, 3 Dec 2018 07:57:05 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22080 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726553AbeLCM5F (ORCPT ); Mon, 3 Dec 2018 07:57:05 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S10; Mon, 03 Dec 2018 20:50:42 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 08/11] rbd: wait image request all complete in lock releasing Date: Mon, 3 Dec 2018 07:50:32 -0500 Message-Id: <1543841435-13652-9-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S10 X-Coremail-Antispam: 1Uf129KBjvJXoWxZry5trykJF4Uur4kAFy5Arb_yoW5ZFyfp3 y3JasIkrWUWwn7Ww1fJayrZr1Yga18t347WryIkw1xCFn3JrWvvF1IkFyUZFW7Ar93Ar4x GF45tFs5CF42grDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbZKZAUUUUU= X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiiwISeltVe-R37QAAs6 Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP currently, we only sync all osdc request in lock releasing, but if we are going to support journaling, we need to wait all img_request complete, not only the low-level in osd_client. Signed-off-by: Dongsheng Yang --- drivers/block/rbd.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index 8e5140b..44d1285 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -386,6 +386,9 @@ struct rbd_device { struct list_head node; + atomic_t inflight_ios; + struct completion inflight_wait; + /* sysfs related */ struct device dev; unsigned long open_count; /* protected by lock */ @@ -1654,6 +1657,7 @@ static struct rbd_img_request *rbd_img_request_create( spin_lock_init(&img_request->completion_lock); INIT_LIST_HEAD(&img_request->object_extents); kref_init(&img_request->kref); + atomic_inc(&rbd_dev->inflight_ios); dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, obj_op_name(op_type), img_request); @@ -1670,6 +1674,8 @@ static void rbd_img_request_destroy(struct kref *kref) dout("%s: img %p\n", __func__, img_request); + atomic_dec(&img_request->rbd_dev->inflight_ios); + complete_all(&img_request->rbd_dev->inflight_wait); for_each_obj_request_safe(img_request, obj_request, next_obj_request) rbd_img_obj_request_del(img_request, obj_request); rbd_assert(img_request->obj_request_count == 0); @@ -3070,26 +3076,39 @@ static void rbd_acquire_lock(struct work_struct *work) } } +static int rbd_inflight_wait(struct rbd_device *rbd_dev) +{ + int ret = 0; + + while (atomic_read(&rbd_dev->inflight_ios)) { + ret = wait_for_completion_interruptible(&rbd_dev->inflight_wait); + if (ret) + break; + } + + return ret; +} + /* * lock_rwsem must be held for write */ static bool rbd_release_lock(struct rbd_device *rbd_dev) { + int ret = 0; + dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, rbd_dev->lock_state); if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) return false; rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; - downgrade_write(&rbd_dev->lock_rwsem); - /* - * Ensure that all in-flight IO is flushed. - * - * FIXME: ceph_osdc_sync() flushes the entire OSD client, which - * may be shared with other devices. - */ - ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); - up_read(&rbd_dev->lock_rwsem); + up_write(&rbd_dev->lock_rwsem); + + ret = rbd_inflight_wait(rbd_dev); + if (ret) { + down_write(&rbd_dev->lock_rwsem); + return false; + } down_write(&rbd_dev->lock_rwsem); dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, @@ -4387,6 +4406,8 @@ static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, INIT_LIST_HEAD(&rbd_dev->node); init_rwsem(&rbd_dev->header_rwsem); + atomic_set(&rbd_dev->inflight_ios, 0); + init_completion(&rbd_dev->inflight_wait); rbd_dev->header.data_pool_id = CEPH_NOPOOL; ceph_oid_init(&rbd_dev->header_oid); rbd_dev->header_oloc.pool = spec->pool_id; From patchwork Mon Dec 3 12:50:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709505 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4890116B1 for ; Mon, 3 Dec 2018 12:56:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3A9D72B415 for ; Mon, 3 Dec 2018 12:56:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E6E52B54C; Mon, 3 Dec 2018 12:56:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C70F62B415 for ; Mon, 3 Dec 2018 12:56:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726592AbeLCM5W (ORCPT ); Mon, 3 Dec 2018 07:57:22 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22068 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726238AbeLCM5V (ORCPT ); Mon, 3 Dec 2018 07:57:21 -0500 X-Greylist: delayed 313 seconds by postgrey-1.27 at vger.kernel.org; Mon, 03 Dec 2018 07:57:00 EST Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S11; Mon, 03 Dec 2018 20:50:42 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 09/11] rbd: introduce completion for each img_request Date: Mon, 3 Dec 2018 07:50:33 -0500 Message-Id: <1543841435-13652-10-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S11 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUlLSlUUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiiQISeltVfCxuZQAAs2 Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we are going to do a sync IO, we need a way to wait a img_request to complete. Example, when we are going to do journal replay, we need to do a sync replaying, and return after img_request completed. Signed-off-by: Dongsheng Yang --- drivers/block/rbd.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index 44d1285..cc9ee8d 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -294,6 +294,8 @@ struct rbd_img_request { u32 obj_request_count; u32 pending_count; + struct completion completion; + struct kref kref; }; @@ -1654,6 +1656,7 @@ static struct rbd_img_request *rbd_img_request_create( if (rbd_dev_parent_get(rbd_dev)) img_request_layered_set(img_request); + init_completion(&img_request->completion); spin_lock_init(&img_request->completion_lock); INIT_LIST_HEAD(&img_request->object_extents); kref_init(&img_request->kref); @@ -2598,6 +2601,7 @@ static void rbd_img_end_request(struct rbd_img_request *img_req) blk_mq_end_request(img_req->rq, errno_to_blk_status(img_req->result)); + complete_all(&img_req->completion); rbd_img_request_put(img_req); } From patchwork Mon Dec 3 12:50:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709501 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1C70D16B1 for ; Mon, 3 Dec 2018 12:56:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E1ED2B54C for ; Mon, 3 Dec 2018 12:56:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 019D02B569; Mon, 3 Dec 2018 12:56:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CE9B2B415 for ; Mon, 3 Dec 2018 12:56:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726587AbeLCM5M (ORCPT ); Mon, 3 Dec 2018 07:57:12 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22061 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726563AbeLCM5L (ORCPT ); Mon, 3 Dec 2018 07:57:11 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S12; Mon, 03 Dec 2018 20:50:43 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 10/11] rbd: introduce a lock_flag in rbd_dev to make some process exclusive-lock protected Date: Mon, 3 Dec 2018 07:50:34 -0500 Message-Id: <1543841435-13652-11-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S12 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUl0eHDUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbiiAMSeltVe+V9mQAAsX Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we are going to open journal, we don't want to release exclusive-lock in the whole process. But we can release it after opening. So introduce a lock_flag in rbd_dev to say, wait, we don't want to release exclusive-lock currently, please wait for this flag cleared. Signed-off-by: Dongsheng Yang --- drivers/block/rbd.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index cc9ee8d..8db4c36 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -316,6 +316,10 @@ enum rbd_lock_state { RBD_LOCK_STATE_RELEASING, }; +enum rbd_lock_flags { + RBD_LOCK_FLAG_RELEASE_WAIT, // wait until this flag cleared in lock releasing +}; + /* WatchNotify::ClientId */ struct rbd_client_id { u64 gid; @@ -363,6 +367,8 @@ struct rbd_device { struct rw_semaphore lock_rwsem; enum rbd_lock_state lock_state; + unsigned long lock_flags; + struct completion lock_wait; char lock_cookie[32]; struct rbd_client_id owner_cid; struct work_struct acquired_lock_work; @@ -3080,6 +3086,19 @@ static void rbd_acquire_lock(struct work_struct *work) } } +static int rbd_lock_wait(struct rbd_device *rbd_dev) +{ + int ret = 0; + + while (test_bit(RBD_LOCK_FLAG_RELEASE_WAIT, &rbd_dev->lock_flags)) { + ret = wait_for_completion_interruptible(&rbd_dev->lock_wait); + if (ret) + break; + } + + return ret; +} + static int rbd_inflight_wait(struct rbd_device *rbd_dev) { int ret = 0; @@ -3108,6 +3127,12 @@ static bool rbd_release_lock(struct rbd_device *rbd_dev) rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; up_write(&rbd_dev->lock_rwsem); + ret = rbd_lock_wait(rbd_dev); + if (ret) { + down_write(&rbd_dev->lock_rwsem); + return false; + } + ret = rbd_inflight_wait(rbd_dev); if (ret) { down_write(&rbd_dev->lock_rwsem); @@ -4428,6 +4453,7 @@ static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, init_rwsem(&rbd_dev->lock_rwsem); rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; + init_completion(&rbd_dev->lock_wait); INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); From patchwork Mon Dec 3 12:50:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 10709509 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2E3A713BF for ; Mon, 3 Dec 2018 12:56:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B2612B415 for ; Mon, 3 Dec 2018 12:56:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0F3A62B54C; Mon, 3 Dec 2018 12:56:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFCE22B415 for ; Mon, 3 Dec 2018 12:56:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726595AbeLCM5m (ORCPT ); Mon, 3 Dec 2018 07:57:42 -0500 Received: from m50211.mail.qiye.163.com ([123.125.50.211]:22183 "EHLO m50211.mail.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726588AbeLCM5m (ORCPT ); Mon, 3 Dec 2018 07:57:42 -0500 Received: from atest-guest.localdomain (unknown [218.94.118.90]) by smtp3 (Coremail) with SMTP id PdOowEDpa0CfJgVcOwAAAA--.14S13; Mon, 03 Dec 2018 20:50:43 +0800 (CST) From: Dongsheng Yang To: idryomov@gmail.com, sage@redhat.com, elder@kernel.org, jdillama@redhat.com Cc: dongsheng.yang@easystack.cn, ceph-devel@vger.kernel.org Subject: [PATCH 11/11] rbd: enable journaling Date: Mon, 3 Dec 2018 07:50:35 -0500 Message-Id: <1543841435-13652-12-git-send-email-dongsheng.yang@easystack.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> References: <1543841435-13652-1-git-send-email-dongsheng.yang@easystack.cn> X-CM-TRANSID: PdOowEDpa0CfJgVcOwAAAA--.14S13 X-Coremail-Antispam: 1Uf129KBjvAXoWfZrW5tFyUJw48Xw4ktF45trb_yoW8ZFyfuo WfGr1fXa1rGFyUAFWkCr4IqFyrWrykG3WFyrWYqa1j9wsF93y09w17Ca13ta4rAF43JFs7 Kr48Xwn5GF4kJw45n29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjfUl0eHDUUUU X-Originating-IP: [218.94.118.90] X-CM-SenderInfo: 5grqw2pkhqwhp1dqwq5hdv52pwdfyhdfq/1tbihQMSelsflXmXkAAAsB Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Dongsheng Yang --- drivers/block/rbd.c | 566 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 559 insertions(+), 7 deletions(-) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index 8db4c36..0ba9750 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -28,16 +28,19 @@ */ +#include #include #include #include #include #include #include +#include #include #include #include +#include #include #include #include @@ -115,12 +118,14 @@ static int atomic_dec_return_safe(atomic_t *v) #define RBD_FEATURE_LAYERING (1ULL<<0) #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) +#define RBD_FEATURE_JOURNALING (1ULL<<6) #define RBD_FEATURE_DATA_POOL (1ULL<<7) #define RBD_FEATURE_OPERATIONS (1ULL<<8) #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ RBD_FEATURE_STRIPINGV2 | \ RBD_FEATURE_EXCLUSIVE_LOCK | \ + RBD_FEATURE_JOURNALING | \ RBD_FEATURE_DATA_POOL | \ RBD_FEATURE_OPERATIONS) @@ -295,6 +300,7 @@ struct rbd_img_request { u32 pending_count; struct completion completion; + uint64_t journaler_commit_tid; struct kref kref; }; @@ -384,6 +390,8 @@ struct rbd_device { atomic_t parent_ref; struct rbd_device *parent; + struct rbd_journal *journal; + /* Block layer tags. */ struct blk_mq_tag_set tag_set; @@ -414,6 +422,15 @@ enum rbd_dev_flags { RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ }; +#define LOCAL_MIRROR_UUID "" +#define LOCAL_CLIENT_ID "" + +struct rbd_journal { + struct ceph_journaler *journaler; + struct ceph_journaler_client *client; + uint64_t tag_tid; +}; + static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ static LIST_HEAD(rbd_dev_list); /* devices */ @@ -2601,12 +2618,20 @@ static void rbd_img_end_child_request(struct rbd_img_request *img_req) static void rbd_img_end_request(struct rbd_img_request *img_req) { rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); - rbd_assert((!img_req->result && - img_req->xferred == blk_rq_bytes(img_req->rq)) || - (img_req->result < 0 && !img_req->xferred)); - blk_mq_end_request(img_req->rq, - errno_to_blk_status(img_req->result)); + if (img_req->rq) { + rbd_assert((!img_req->result && + img_req->xferred == blk_rq_bytes(img_req->rq)) || + (img_req->result < 0 && !img_req->xferred)); + blk_mq_end_request(img_req->rq, + errno_to_blk_status(img_req->result)); + } + + if (img_req->journaler_commit_tid) { + ceph_journaler_client_committed(img_req->rbd_dev->journal->journaler, + img_req->journaler_commit_tid); + } + complete_all(&img_req->completion); rbd_img_request_put(img_req); } @@ -3674,6 +3699,19 @@ static int rbd_wait_state_locked(struct rbd_device *rbd_dev, bool may_acquire) return ret; } +struct rbd_journal_ctx { + struct rbd_device *rbd_dev; + struct rbd_img_request *img_request; + struct request *rq; + struct ceph_snap_context *snapc; + int result; + bool must_be_locked; +}; + +static int rbd_journal_append(struct rbd_device *rbd_dev, struct bio *bio, + u64 offset, u64 length, enum obj_operation_type op_type, + struct rbd_journal_ctx *ctx); + static void rbd_queue_workfn(struct work_struct *work) { struct request *rq = blk_mq_rq_from_pdu(work); @@ -3779,7 +3817,29 @@ static void rbd_queue_workfn(struct work_struct *work) if (result) goto err_img_request; - rbd_img_request_submit(img_request); + if (!(rbd_dev->header.features & RBD_FEATURE_JOURNALING) || + (op_type == OBJ_OP_READ)) { + rbd_img_request_submit(img_request); + } else { + struct rbd_journal_ctx *ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); + + if (!ctx){ + result = -ENOMEM; + goto err_unlock; + } + + ctx->img_request = img_request; + ctx->rq = rq; + ctx->snapc = snapc; + ctx->must_be_locked = must_be_locked; + ctx->rbd_dev = rbd_dev; + result = rbd_journal_append(rbd_dev, rq->bio, offset, length, op_type, ctx); + if (result) { + pr_err("error in rbd_journal_append"); + goto err_unlock; + } + } + if (must_be_locked) up_read(&rbd_dev->lock_rwsem); return; @@ -5730,6 +5790,7 @@ static int rbd_dev_device_setup(struct rbd_device *rbd_dev) if (ret) goto err_out_disk; + set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); @@ -5771,6 +5832,336 @@ static int rbd_dev_header_name(struct rbd_device *rbd_dev) return ret; } +enum rbd_journal_event_type { + EVENT_TYPE_AIO_DISCARD = 0, + EVENT_TYPE_AIO_WRITE = 1, + EVENT_TYPE_AIO_FLUSH = 2, + EVENT_TYPE_OP_FINISH = 3, + EVENT_TYPE_SNAP_CREATE = 4, + EVENT_TYPE_SNAP_REMOVE = 5, + EVENT_TYPE_SNAP_RENAME = 6, + EVENT_TYPE_SNAP_PROTECT = 7, + EVENT_TYPE_SNAP_UNPROTECT = 8, + EVENT_TYPE_SNAP_ROLLBACK = 9, + EVENT_TYPE_RENAME = 10, + EVENT_TYPE_RESIZE = 11, + EVENT_TYPE_FLATTEN = 12, + EVENT_TYPE_DEMOTE_PROMOTE = 13, + EVENT_TYPE_SNAP_LIMIT = 14, + EVENT_TYPE_UPDATE_FEATURES = 15, + EVENT_TYPE_METADATA_SET = 16, + EVENT_TYPE_METADATA_REMOVE = 17, + EVENT_TYPE_AIO_WRITESAME = 18, + EVENT_TYPE_AIO_COMPARE_AND_WRITE = 19, +}; + +static struct bio_vec *setup_write_bvecs(void *buf, u64 offset, u64 length) +{ + u32 i; + struct bio_vec *bvecs = NULL; + u32 bvec_count = 0; + + bvec_count = calc_pages_for(offset, length); + bvecs = kcalloc(bvec_count, sizeof(*bvecs), GFP_NOIO); + if (!bvecs) + goto err; + + offset = offset % PAGE_SIZE; + for (i = 0; i < bvec_count; i++) { + unsigned int len = min(length, (u64)PAGE_SIZE - offset); + + bvecs[i].bv_page = alloc_page(GFP_NOIO); + if (!bvecs[i].bv_page) + goto free_bvecs; + + bvecs[i].bv_offset = offset; + bvecs[i].bv_len = len; + memcpy(page_address(bvecs[i].bv_page) + bvecs[i].bv_offset, buf, bvecs[i].bv_len); + length -= len; + buf += len; + offset = 0; + } + + rbd_assert(!length); + + return bvecs; + +free_bvecs: +err: + return NULL; +} + +static int rbd_journal_handle_aio_discard(struct rbd_device *rbd_dev, void **p, void *end, u8 struct_v, uint64_t commit_tid) +{ + uint64_t offset; + uint64_t length; + int result = 0; + enum obj_operation_type op_type; + struct rbd_img_request *img_request; + struct ceph_snap_context *snapc = NULL; + + offset = ceph_decode_64(p); + length = ceph_decode_64(p); + + snapc = rbd_dev->header.snapc; + ceph_get_snap_context(snapc); + op_type = OBJ_OP_WRITE; + + img_request = rbd_img_request_create(rbd_dev, op_type, snapc); + if (!img_request) { + result = -ENOMEM; + goto err; + } + img_request->journaler_commit_tid = commit_tid; + + result = rbd_img_fill_nodata(img_request, offset, length); + if (result) + goto err; + + rbd_img_request_submit(img_request); + result = wait_for_completion_interruptible(&img_request->completion); +err: + return result; +} + +static int rbd_journal_handle_aio_write(struct rbd_device *rbd_dev, void **p, void *end, u8 struct_v, uint64_t commit_tid) +{ + uint64_t offset; + uint64_t length; + char *data; + ssize_t data_len; + int result = 0; + enum obj_operation_type op_type; + struct ceph_snap_context *snapc = NULL; + struct rbd_img_request *img_request; + + struct ceph_file_extent ex; + struct bio_vec *bvecs; + + offset = ceph_decode_64(p); + length = ceph_decode_64(p); + + data = ceph_extract_encoded_string(p, end, &data_len, GFP_NOIO); + if (!data) + return -ENOMEM; + + snapc = rbd_dev->header.snapc; + ceph_get_snap_context(snapc); + op_type = OBJ_OP_DISCARD; + + img_request = rbd_img_request_create(rbd_dev, op_type, snapc); + if (!img_request) { + result = -ENOMEM; + goto err; + } + + img_request->journaler_commit_tid = commit_tid; + snapc = NULL; /* img_request consumes a ref */ + + ex.fe_off = offset; + ex.fe_len = length; + + bvecs = setup_write_bvecs(data, offset, length); + if (!bvecs) + pr_err("failed to alloc bvecs."); + result = rbd_img_fill_from_bvecs(img_request, + &ex, 1, bvecs); + if (result) + goto err; + + rbd_img_request_submit(img_request); + result = wait_for_completion_interruptible(&img_request->completion); + +err: + return result; +} + +static int rbd_journal_replay(void *entry_handler, struct ceph_journaler_entry *entry, uint64_t commit_tid) +{ + struct rbd_device *rbd_dev = entry_handler; + void **p = (void **)(&entry->data); + void *end = *p + entry->data_len; + uint32_t event_type; + u8 struct_v; + u32 struct_len; + int ret = 0; + + ret = ceph_start_decoding(p, end, 1, "rbd_decode_entry", + &struct_v, &struct_len); + if (ret) + return -EINVAL; + + event_type = ceph_decode_32(p); + + switch (event_type) { + case EVENT_TYPE_AIO_WRITE: + rbd_journal_handle_aio_write(rbd_dev, p, end, struct_v, commit_tid); + break; + case EVENT_TYPE_AIO_DISCARD: + rbd_journal_handle_aio_discard(rbd_dev, p, end, struct_v, commit_tid); + break; + case EVENT_TYPE_AIO_FLUSH: + break; + default: + pr_err("unknown event_type: %u", event_type); + return -EINVAL; + } + return 0; +} + +static int rbd_journal_allocate_tag(struct rbd_journal *journal); +static int rbd_dev_open_journal(struct rbd_device *rbd_dev) +{ + int ret = 0; + struct rbd_journal *journal = NULL; + struct ceph_journaler *journaler = NULL; + struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; + + journal = kzalloc(sizeof(struct rbd_journal), GFP_KERNEL); + if (!journal) + return -ENOMEM; + + journaler = ceph_journaler_create(osdc, &rbd_dev->header_oloc, + rbd_dev->spec->image_id, + LOCAL_CLIENT_ID); + if (!journaler) { + ret = -ENOMEM; + goto err_free_journal; + } + + journaler->entry_handler = rbd_dev; + journaler->handle_entry = rbd_journal_replay; + + ret = ceph_journaler_open(journaler); + if (ret) + goto err_destroy_journaler; + + journal->journaler = journaler; + ret = ceph_journaler_start_replay(journaler); + if (ret) + goto err_close_journaler; + + rbd_dev->journal = journal; + ret = rbd_journal_allocate_tag(journal); + if (ret) + goto err_close_journaler; + + return ret; + +err_close_journaler: + ceph_journaler_close(journaler); +err_destroy_journaler: + ceph_journaler_destroy(journaler); +err_free_journal: + kfree(rbd_dev->journal); + rbd_dev->journal = NULL; + return ret; +} + +static void rbd_dev_close_journal(struct rbd_device *rbd_dev) +{ + struct ceph_journaler *journaler = NULL; + + if (!rbd_dev->journal) + return; + + journaler = rbd_dev->journal->journaler; + ceph_journaler_close(journaler); + ceph_journaler_destroy(journaler); + kfree(rbd_dev->journal); + rbd_dev->journal = NULL; +} + +typedef struct rbd_journal_tag_predecessor { + bool commit_valid; + uint64_t tag_tid; + uint64_t entry_tid; + uint32_t uuid_len; + char *mirror_uuid; +} rbd_journal_tag_predecessor; + +typedef struct rbd_journal_tag_data { + struct rbd_journal_tag_predecessor predecessor; + uint32_t uuid_len; + char *mirror_uuid; +} rbd_journal_tag_data; + +static uint32_t tag_data_encoding_size(struct rbd_journal_tag_data *tag_data) +{ + // sizeof(uuid_len) 4 + uuid_len + 1 commit_valid + 8 tag_tid + 8 entry_tid + 4 sizeof(uuid_len) + uuid_len + return (4 + tag_data->uuid_len + 1 + 8 + 8 + 4 + tag_data->predecessor.uuid_len); +} + +static void predecessor_encode(void **p, void *end, struct rbd_journal_tag_predecessor *predecessor) +{ + ceph_encode_string(p, end, predecessor->mirror_uuid, predecessor->uuid_len); + ceph_encode_8(p, predecessor->commit_valid); + ceph_encode_64(p, predecessor->tag_tid); + ceph_encode_64(p, predecessor->entry_tid); +} + +static int rbd_journal_encode_tag_data(void **p, void *end, struct rbd_journal_tag_data *tag_data) +{ + struct rbd_journal_tag_predecessor *predecessor = &tag_data->predecessor; + + ceph_encode_string(p, end, tag_data->mirror_uuid, tag_data->uuid_len); + predecessor_encode(p, end, predecessor); + + return 0; +} + +static int rbd_journal_allocate_tag(struct rbd_journal *journal) +{ + struct ceph_journaler_tag tag; + struct ceph_journaler *journaler = journal->journaler; + struct ceph_journaler_client *client; + struct rbd_journal_tag_predecessor *predecessor; + struct ceph_journaler_object_pos *position; + struct rbd_journal_tag_data tag_data; + void *buf = NULL, *p = NULL, *end = NULL; + uint32_t buf_len; + int ret = 0; + + ret = ceph_journaler_get_cached_client(journaler, LOCAL_CLIENT_ID, &client); + if (ret) + goto out; + + predecessor = &tag_data.predecessor; + position = list_first_entry(&client->object_positions, struct ceph_journaler_object_pos, node); + + predecessor->commit_valid = true; + predecessor->tag_tid = position->tag_tid; + predecessor->entry_tid = position->entry_tid; + predecessor->uuid_len = 0; + predecessor->mirror_uuid = LOCAL_MIRROR_UUID; + + tag_data.uuid_len = 0; + tag_data.mirror_uuid = LOCAL_MIRROR_UUID; + + buf_len = tag_data_encoding_size(&tag_data); + + p = kmalloc(buf_len, GFP_KERNEL); + if (!p) { + pr_err("failed to allocate tag data"); + return -ENOMEM; + } + + end = p + buf_len; + buf = p; + ret = rbd_journal_encode_tag_data(&p, end, &tag_data); + if (ret) { + pr_err("error in tag data"); + return ret; + } + + ret = ceph_journaler_allocate_tag(journaler, 0, buf, buf_len, &tag); + + journal->tag_tid = tag.tid; + +out: + return ret; +} + static void rbd_dev_image_release(struct rbd_device *rbd_dev) { rbd_dev_unprobe(rbd_dev); @@ -5781,6 +6172,145 @@ static void rbd_dev_image_release(struct rbd_device *rbd_dev) rbd_dev->spec->image_id = NULL; } +struct AioWriteEvent { + enum rbd_journal_event_type type; + uint64_t offset; + uint64_t length; + char data[0]; +}; + + +static void rbd_journal_callback(struct ceph_journaler_ctx *journaler_ctx) +{ + struct rbd_journal_ctx *ctx = journaler_ctx->priv; + int result = journaler_ctx->result; + struct rbd_device *rbd_dev = ctx->rbd_dev; + bool must_be_locked = ctx->must_be_locked; + + if (result) + goto err_rq; + + if (must_be_locked) + down_read(&rbd_dev->lock_rwsem); + + rbd_img_request_submit(ctx->img_request); + + if (must_be_locked) + up_read(&rbd_dev->lock_rwsem); + + kfree(ctx); + return; + +err_rq: + ceph_put_snap_context(ctx->snapc); + blk_mq_end_request(ctx->rq, errno_to_blk_status(result)); + rbd_img_request_put(ctx->img_request); + kfree(ctx); +} + +static int rbd_journal_append_write_event(struct rbd_device *rbd_dev, struct bio *bio, + u64 offset, u64 length, struct rbd_journal_ctx *ctx) +{ + void *p = NULL; + struct ceph_journaler_ctx *journaler_ctx; + struct ceph_bio_iter *bio_iter; + + bio_iter = kzalloc(sizeof(struct ceph_bio_iter), GFP_KERNEL); + bio_iter->bio = bio; + bio_iter->iter = bio->bi_iter; + bio_iter->bio_len = length; + + bio_iter->prefix_page = alloc_page(GFP_KERNEL); + memset(page_address(bio_iter->prefix_page), 0, PAGE_SIZE); + + // EVENT_FIXED_SIZE(10 = CEPH_ENCODING_START_BLK_LEN(6) + EVENT_TYPE(4)) + + // offset(8) + length(8) + string_len(4) = 30 + bio_iter->prefix_len = 30; + bio_iter->prefix_offset = PAGE_SIZE - bio_iter->prefix_len; + + p = page_address(bio_iter->prefix_page) + bio_iter->prefix_offset; + + journaler_ctx = kmalloc(sizeof(*journaler_ctx), GFP_KERNEL); + if (!journaler_ctx) { + return -ENOMEM; + } + + ceph_start_encoding(&p, 1, 1, bio_iter->prefix_len + bio_iter->bio_len - 6); + + ceph_encode_32(&p, EVENT_TYPE_AIO_WRITE); + + ceph_encode_64(&p, offset); + ceph_encode_64(&p, length); + + // first part of ceph_encode_string(); + ceph_encode_32(&p, bio_iter->bio_len); + + journaler_ctx->priv = ctx; + journaler_ctx->callback = rbd_journal_callback; + journaler_ctx->bio_iter = bio_iter; + + ceph_journaler_append(rbd_dev->journal->journaler, rbd_dev->journal->tag_tid, + &ctx->img_request->journaler_commit_tid, journaler_ctx); + return 0; +} + +static int rbd_journal_append_discard_event(struct rbd_device *rbd_dev, struct bio *bio, + u64 offset, u64 length, struct rbd_journal_ctx *ctx) +{ + void *p = NULL; + void *buf = NULL; + struct ceph_journaler_ctx *journaler_ctx; + struct ceph_bio_iter *bio_iter; + + bio_iter = kzalloc(sizeof(struct ceph_bio_iter), GFP_KERNEL); + bio_iter->bio = bio; + bio_iter->iter = bio->bi_iter; + bio_iter->bio_len = 0; + + bio_iter->prefix_page = alloc_page(GFP_KERNEL); + // EVENT_FIXED_SIZE(10 = CEPH_ENCODING_START_BLK_LEN(6) + EVENT_TYPE(4)) + + // offset(8) + length(8) = 26 + bio_iter->prefix_len = 26; + bio_iter->prefix_offset = PAGE_SIZE - bio_iter->prefix_len; + + p = page_address(bio_iter->prefix_page) + bio_iter->prefix_offset; + buf = p + bio_iter->prefix_len; + + journaler_ctx = kmalloc(sizeof(*journaler_ctx), GFP_KERNEL); + if (!journaler_ctx) { + return -ENOMEM; + } + + ceph_start_encoding(&p, 1, 1, bio_iter->prefix_len + bio_iter->bio_len - 6); + + ceph_encode_32(&p, EVENT_TYPE_AIO_DISCARD); + + ceph_encode_64(&p, offset); + ceph_encode_64(&p, length); + + journaler_ctx->priv = ctx; + journaler_ctx->callback = rbd_journal_callback; + journaler_ctx->bio_iter = bio_iter; + + ceph_journaler_append(rbd_dev->journal->journaler, rbd_dev->journal->tag_tid, + &ctx->img_request->journaler_commit_tid, journaler_ctx); + return 0; +} + +static int rbd_journal_append(struct rbd_device *rbd_dev, struct bio *bio, + u64 offset, u64 length, enum obj_operation_type op_type, + struct rbd_journal_ctx *ctx) +{ + switch (op_type) { + case OBJ_OP_WRITE: + return rbd_journal_append_write_event(rbd_dev, bio, offset, length, ctx); + case OBJ_OP_DISCARD: + return rbd_journal_append_discard_event(rbd_dev, bio, offset, length, ctx); + default: + return 0; + } +} + /* * Probe for the existence of the header object for the given rbd * device. If this image is the one being mapped (i.e., not a @@ -5947,11 +6477,30 @@ static ssize_t do_rbd_add(struct bus_type *bus, goto err_out_device_setup; } + if (rbd_dev->header.features & RBD_FEATURE_JOURNALING) { + set_bit(RBD_LOCK_FLAG_RELEASE_WAIT, &rbd_dev->lock_flags); + rc = rbd_add_acquire_lock(rbd_dev); + if (rc) { + clear_bit(RBD_LOCK_FLAG_RELEASE_WAIT, &rbd_dev->lock_flags); + complete_all(&rbd_dev->lock_wait); + goto err_out_device_setup; + } + + rc = rbd_dev_open_journal(rbd_dev); + if (rc) { + clear_bit(RBD_LOCK_FLAG_RELEASE_WAIT, &rbd_dev->lock_flags); + complete_all(&rbd_dev->lock_wait); + goto err_out_image_lock; + } + clear_bit(RBD_LOCK_FLAG_RELEASE_WAIT, &rbd_dev->lock_flags); + complete_all(&rbd_dev->lock_wait); + } + /* Everything's ready. Announce the disk to the world. */ rc = device_add(&rbd_dev->dev); if (rc) - goto err_out_image_lock; + goto err_out_close_journal; add_disk(rbd_dev->disk); /* see rbd_init_disk() */ @@ -5969,6 +6518,8 @@ static ssize_t do_rbd_add(struct bus_type *bus, module_put(THIS_MODULE); return rc; +err_out_close_journal: + rbd_dev_close_journal(rbd_dev); err_out_image_lock: rbd_dev_image_unlock(rbd_dev); err_out_device_setup: @@ -6095,6 +6646,7 @@ static ssize_t do_rbd_remove(struct bus_type *bus, device_del(&rbd_dev->dev); rbd_dev_image_unlock(rbd_dev); + rbd_dev_close_journal(rbd_dev); rbd_dev_device_release(rbd_dev); rbd_dev_image_release(rbd_dev); rbd_dev_destroy(rbd_dev);