All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: ceph-devel@vger.kernel.org
Cc: xiubli@redhat.com, lhenriques@suse.de, idryomov@gmail.com
Subject: [PATCH v14 05/64] libceph: support sparse reads on msgr2 secure codepath
Date: Wed, 27 Apr 2022 15:12:15 -0400	[thread overview]
Message-ID: <20220427191314.222867-6-jlayton@kernel.org> (raw)
In-Reply-To: <20220427191314.222867-1-jlayton@kernel.org>

Add a new init_sgs_pages helper that populates the scatterlist from
an arbitrary point in an array of pages.

Change setup_message_sgs to take an optional pointer to an array of
pages. If that's set, then the scatterlist will be set using that
array instead of the cursor.

When given a sparse read on a secure connection, decrypt the data
in-place rather than into the final destination, by passing it the
in_enc_pages array.

After decrypting, run the sparse_read state machine in a loop, copying
data from the decrypted pages until it's complete.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 net/ceph/messenger_v2.c | 119 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 109 insertions(+), 10 deletions(-)

diff --git a/net/ceph/messenger_v2.c b/net/ceph/messenger_v2.c
index d527777af584..3dcaee6f8903 100644
--- a/net/ceph/messenger_v2.c
+++ b/net/ceph/messenger_v2.c
@@ -963,12 +963,48 @@ static void init_sgs_cursor(struct scatterlist **sg,
 	}
 }
 
+/**
+ * init_sgs_pages: set up scatterlist on an array of page pointers
+ * @sg: 	scatterlist to populate
+ * @pages: 	pointer to page array
+ * @dpos:	position in the array to start (bytes)
+ * @dlen:	len to add to sg (bytes)
+ * @pad:	pointer to pad destination (if any)
+ *
+ * Populate the scatterlist from the page array, starting at an arbitrary
+ * byte in the array and running for a specified length.
+ */
+static void init_sgs_pages(struct scatterlist **sg, struct page **pages,
+			     int dpos, int dlen, u8 *pad)
+{
+	int idx = dpos >> PAGE_SHIFT;
+	int off = offset_in_page(dpos);
+	int resid = dlen;
+
+	do {
+		int len = min(resid, (int)PAGE_SIZE - off);
+
+		sg_set_page(*sg, pages[idx], len, off);
+		*sg = sg_next(*sg);
+		off = 0;
+		++idx;
+		resid -= len;
+	} while (resid);
+
+	if (need_padding(dlen)) {
+		sg_set_buf(*sg, pad, padding_len(dlen));
+		*sg = sg_next(*sg);
+	}
+}
+
 static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
 			     u8 *front_pad, u8 *middle_pad, u8 *data_pad,
-			     void *epilogue, bool add_tag)
+			     void *epilogue, struct page **pages, int dpos,
+			     bool add_tag)
 {
 	struct ceph_msg_data_cursor cursor;
 	struct scatterlist *cur_sg;
+	int dlen = data_len(msg);
 	int sg_cnt;
 	int ret;
 
@@ -982,9 +1018,15 @@ static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
 	if (middle_len(msg))
 		sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base,
 				      middle_len(msg));
-	if (data_len(msg)) {
-		ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
-		sg_cnt += calc_sg_cnt_cursor(&cursor);
+	if (dlen) {
+		if (pages) {
+			sg_cnt += calc_pages_for(dpos, dlen);
+			if (need_padding(dlen))
+				sg_cnt++;
+		} else {
+			ceph_msg_data_cursor_init(&cursor, msg, dlen);
+			sg_cnt += calc_sg_cnt_cursor(&cursor);
+		}
 	}
 
 	ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO);
@@ -998,9 +1040,13 @@ static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
 	if (middle_len(msg))
 		init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg),
 			 middle_pad);
-	if (data_len(msg)) {
-		ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
-		init_sgs_cursor(&cur_sg, &cursor, data_pad);
+	if (dlen) {
+		if (pages) {
+			init_sgs_pages(&cur_sg, pages, dpos, dlen, data_pad);
+		} else {
+			ceph_msg_data_cursor_init(&cursor, msg, dlen);
+			init_sgs_cursor(&cur_sg, &cursor, data_pad);
+		}
 	}
 
 	WARN_ON(!sg_is_last(cur_sg));
@@ -1035,10 +1081,52 @@ static int decrypt_control_remainder(struct ceph_connection *con)
 			 padded_len(rem_len) + CEPH_GCM_TAG_LEN);
 }
 
+/* Process sparse read data that lives in a buffer */
+static int process_v2_sparse_read(struct ceph_connection *con, struct page **pages, int spos)
+{
+	struct ceph_msg_data_cursor *cursor = &con->v2.in_cursor;
+	int ret;
+
+	for (;;) {
+		char *buf = NULL;
+
+		ret = con->ops->sparse_read(con, cursor, &buf);
+		if (ret <= 0)
+			return ret;
+
+		dout("%s: sparse_read return %x buf %p\n", __func__, ret, buf);
+
+		do {
+			int idx = spos >> PAGE_SHIFT;
+			int soff = offset_in_page(spos);
+			struct page *spage = con->v2.in_enc_pages[idx];
+			int len = min_t(int, ret, PAGE_SIZE - soff);
+
+			if (buf) {
+				memcpy_from_page(buf, spage, soff, len);
+				buf += len;
+			} else {
+				struct bio_vec bv;
+
+				get_bvec_at(cursor, &bv);
+				len = min_t(int, len, bv.bv_len);
+				memcpy_page(bv.bv_page, bv.bv_offset,
+					    spage, soff, len);
+				ceph_msg_data_advance(cursor, len);
+			}
+			spos += len;
+			ret -= len;
+		} while (ret);
+	}
+}
+
 static int decrypt_tail(struct ceph_connection *con)
 {
 	struct sg_table enc_sgt = {};
 	struct sg_table sgt = {};
+	struct page **pages = NULL;
+	bool sparse = con->in_msg->sparse_read;
+	int dpos = 0;
 	int tail_len;
 	int ret;
 
@@ -1049,9 +1137,14 @@ static int decrypt_tail(struct ceph_connection *con)
 	if (ret)
 		goto out;
 
+	if (sparse) {
+		dpos = padded_len(front_len(con->in_msg) + padded_len(middle_len(con->in_msg)));
+		pages = con->v2.in_enc_pages;
+	}
+
 	ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf),
-			MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
-			con->v2.in_buf, true);
+				MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
+				con->v2.in_buf, pages, dpos, true);
 	if (ret)
 		goto out;
 
@@ -1061,6 +1154,12 @@ static int decrypt_tail(struct ceph_connection *con)
 	if (ret)
 		goto out;
 
+	if (sparse && data_len(con->in_msg)) {
+		ret = process_v2_sparse_read(con, con->v2.in_enc_pages, dpos);
+		if (ret)
+			goto out;
+	}
+
 	WARN_ON(!con->v2.in_enc_page_cnt);
 	ceph_release_page_vector(con->v2.in_enc_pages,
 				 con->v2.in_enc_page_cnt);
@@ -1584,7 +1683,7 @@ static int prepare_message_secure(struct ceph_connection *con)
 
 	encode_epilogue_secure(con, false);
 	ret = setup_message_sgs(&sgt, con->out_msg, zerop, zerop, zerop,
-				&con->v2.out_epil, false);
+				&con->v2.out_epil, NULL, 0, false);
 	if (ret)
 		goto out;
 
-- 
2.35.1


  parent reply	other threads:[~2022-04-27 19:16 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27 19:12 [PATCH v14 00/64] ceph+fscrypt: full support Jeff Layton
2022-04-27 19:12 ` [PATCH v14 01/64] libceph: add spinlock around osd->o_requests Jeff Layton
2022-04-27 19:12 ` [PATCH v14 02/64] libceph: define struct ceph_sparse_extent and add some helpers Jeff Layton
2022-04-27 19:12 ` [PATCH v14 03/64] libceph: add sparse read support to msgr2 crc state machine Jeff Layton
2022-04-27 19:12 ` [PATCH v14 04/64] libceph: add sparse read support to OSD client Jeff Layton
2022-04-27 19:12 ` Jeff Layton [this message]
2022-04-27 19:12 ` [PATCH v14 06/64] libceph: add sparse read support to msgr1 Jeff Layton
2022-04-27 19:12 ` [PATCH v14 07/64] ceph: add new mount option to enable sparse reads Jeff Layton
2022-04-27 19:12 ` [PATCH v14 08/64] fs: change test in inode_insert5 for adding to the sb list Jeff Layton
2022-04-27 19:12 ` [PATCH v14 09/64] fscrypt: export fscrypt_fname_encrypt and fscrypt_fname_encrypted_size Jeff Layton
2022-04-27 19:12 ` [PATCH v14 10/64] fscrypt: add fscrypt_context_for_new_inode Jeff Layton
2022-04-27 19:12 ` [PATCH v14 11/64] ceph: preallocate inode for ops that may create one Jeff Layton
2022-04-27 19:12 ` [PATCH v14 12/64] ceph: fscrypt_auth handling for ceph Jeff Layton
2022-04-27 19:12 ` [PATCH v14 13/64] ceph: ensure that we accept a new context from MDS for new inodes Jeff Layton
2022-04-27 19:12 ` [PATCH v14 14/64] ceph: add support for fscrypt_auth/fscrypt_file to cap messages Jeff Layton
2022-04-27 19:12 ` [PATCH v14 15/64] ceph: implement -o test_dummy_encryption mount option Jeff Layton
2022-04-27 19:12 ` [PATCH v14 16/64] ceph: decode alternate_name in lease info Jeff Layton
2022-04-27 19:12 ` [PATCH v14 17/64] ceph: add fscrypt ioctls Jeff Layton
2022-04-27 19:12 ` [PATCH v14 18/64] ceph: make the ioctl cmd more readable in debug log Jeff Layton
2022-04-27 19:12 ` [PATCH v14 19/64] ceph: make ceph_msdc_build_path use ref-walk Jeff Layton
2022-04-27 19:12 ` [PATCH v14 20/64] ceph: add base64 endcoding routines for encrypted names Jeff Layton
2022-04-27 19:12 ` [PATCH v14 21/64] ceph: add encrypted fname handling to ceph_mdsc_build_path Jeff Layton
2022-04-27 19:12 ` [PATCH v14 22/64] ceph: send altname in MClientRequest Jeff Layton
2022-04-27 19:12 ` [PATCH v14 23/64] ceph: encode encrypted name in dentry release Jeff Layton
2022-04-27 19:12 ` [PATCH v14 24/64] ceph: properly set DCACHE_NOKEY_NAME flag in lookup Jeff Layton
2022-04-27 19:12 ` [PATCH v14 25/64] ceph: set DCACHE_NOKEY_NAME in atomic open Jeff Layton
2022-04-27 19:12 ` [PATCH v14 26/64] ceph: make d_revalidate call fscrypt revalidator for encrypted dentries Jeff Layton
2022-04-27 19:12 ` [PATCH v14 27/64] ceph: add helpers for converting names for userland presentation Jeff Layton
2022-04-27 19:12 ` [PATCH v14 28/64] ceph: fix base64 encoded name's length check in ceph_fname_to_usr() Jeff Layton
2022-04-27 19:12 ` [PATCH v14 29/64] ceph: add fscrypt support to ceph_fill_trace Jeff Layton
2022-04-27 19:12 ` [PATCH v14 30/64] ceph: pass the request to parse_reply_info_readdir() Jeff Layton
2022-04-27 19:12 ` [PATCH v14 31/64] ceph: add ceph_encode_encrypted_dname() helper Jeff Layton
2022-04-27 19:12 ` [PATCH v14 32/64] ceph: add support to readdir for encrypted filenames Jeff Layton
2022-04-27 19:12 ` [PATCH v14 33/64] ceph: create symlinks with encrypted and base64-encoded targets Jeff Layton
2022-04-27 19:12 ` [PATCH v14 34/64] ceph: make ceph_get_name decrypt filenames Jeff Layton
2022-04-27 19:12 ` [PATCH v14 35/64] ceph: add a new ceph.fscrypt.auth vxattr Jeff Layton
2022-04-27 19:12 ` [PATCH v14 36/64] ceph: add some fscrypt guardrails Jeff Layton
2022-04-27 19:12 ` [PATCH v14 37/64] ceph: don't allow changing layout on encrypted files/directories Jeff Layton
2022-04-27 19:12 ` [PATCH v14 38/64] libceph: add CEPH_OSD_OP_ASSERT_VER support Jeff Layton
2022-04-27 19:12 ` [PATCH v14 39/64] ceph: size handling for encrypted inodes in cap updates Jeff Layton
2022-04-27 19:12 ` [PATCH v14 40/64] ceph: fscrypt_file field handling in MClientRequest messages Jeff Layton
2022-04-27 19:12 ` [PATCH v14 41/64] ceph: get file size from fscrypt_file when present in inode traces Jeff Layton
2022-04-27 19:12 ` [PATCH v14 42/64] ceph: handle fscrypt fields in cap messages from MDS Jeff Layton
2022-04-27 19:12 ` [PATCH v14 43/64] ceph: update WARN_ON message to pr_warn Jeff Layton
2022-04-27 19:12 ` [PATCH v14 44/64] ceph: add __ceph_get_caps helper support Jeff Layton
2022-04-27 19:12 ` [PATCH v14 45/64] ceph: add __ceph_sync_read " Jeff Layton
2022-04-27 19:12 ` [PATCH v14 46/64] ceph: add object version support for sync read Jeff Layton
2022-04-27 19:12 ` [PATCH v14 47/64] ceph: add infrastructure for file encryption and decryption Jeff Layton
2022-04-27 19:12 ` [PATCH v14 48/64] ceph: add truncate size handling support for fscrypt Jeff Layton
2022-04-27 19:12 ` [PATCH v14 49/64] libceph: allow ceph_osdc_new_request to accept a multi-op read Jeff Layton
2022-04-27 19:13 ` [PATCH v14 50/64] ceph: disable fallocate for encrypted inodes Jeff Layton
2022-04-27 19:13 ` [PATCH v14 51/64] ceph: disable copy offload on " Jeff Layton
2022-04-27 19:13 ` [PATCH v14 52/64] ceph: don't use special DIO path for " Jeff Layton
2022-04-27 19:13 ` [PATCH v14 53/64] ceph: align data in pages in ceph_sync_write Jeff Layton
2022-04-27 19:13 ` [PATCH v14 54/64] ceph: add read/modify/write to ceph_sync_write Jeff Layton
2022-04-27 19:13 ` [PATCH v14 55/64] ceph: plumb in decryption during sync reads Jeff Layton
2022-04-27 19:13 ` [PATCH v14 56/64] ceph: add fscrypt decryption support to ceph_netfs_issue_op Jeff Layton
2022-04-27 19:13 ` [PATCH v14 57/64] ceph: set i_blkbits to crypto block size for encrypted inodes Jeff Layton
2022-04-27 19:13 ` [PATCH v14 58/64] ceph: add encryption support to writepage Jeff Layton
2022-05-05  9:34   ` Xiubo Li
2022-05-05 10:53     ` Jeff Layton
2022-05-05 11:05       ` Xiubo Li
2022-05-05 11:12         ` Jeff Layton
2022-05-05 11:27           ` Xiubo Li
2022-06-02 16:08   ` Jeff Layton
2022-06-03  9:17     ` Luís Henriques
2022-06-03 12:24       ` Jeff Layton
2022-06-03 12:48         ` Xiubo Li
2022-06-03 11:33     ` Xiubo Li
2022-04-27 19:13 ` [PATCH v14 59/64] ceph: fscrypt support for writepages Jeff Layton
2022-04-27 19:13 ` [PATCH v14 60/64] ceph: invalidate pages when doing direct/sync writes Jeff Layton
2022-04-27 19:13 ` [PATCH v14 61/64] ceph: add support for encrypted snapshot names Jeff Layton
2022-04-27 19:13 ` [PATCH v14 62/64] ceph: add support for handling " Jeff Layton
2022-04-27 19:13 ` [PATCH v14 63/64] ceph: update documentation regarding snapshot naming limitations Jeff Layton
2022-04-27 19:13 ` [PATCH v14 64/64] ceph: prevent snapshots to be created in encrypted locked directories Jeff Layton
2022-05-09 11:53 ` [PATCH v14 00/64] ceph+fscrypt: full support Xiubo Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220427191314.222867-6-jlayton@kernel.org \
    --to=jlayton@kernel.org \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=lhenriques@suse.de \
    --cc=xiubli@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.