ceph: remaining patches for fscrypt support
Subsystems (3)
v15 cover ceph: remaining patches for fscrypt support
v15: rebase onto current ceph/testing branch
add some missing ceph_osdc_wait_request calls
This patchset represents the remaining patches to add fscrypt support, rebased on top of today's "testing" branch. They're all ceph changes as the vfs and fscrypt patches are now merged!
Note that the current ceph-client/wip-fscrypt branch is broken. It's still based on v5.19-rc6 and has some missing calls to ceph_osdc_wait_request that cause panics. This set fixes that.
These are currently in my ceph-fscrypt branch:
https://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux.git/log/?h=ceph-fscrypt
It would be good to update the wip-fscrypt branch with this pile.
Jeff Layton (19):
libceph: add CEPH_OSD_OP_ASSERT_VER support
ceph: size handling for encrypted inodes in cap updates
ceph: fscrypt_file field handling in MClientRequest messages
ceph: get file size from fscrypt_file when present in inode traces
ceph: handle fscrypt fields in cap messages from MDS
ceph: update WARN_ON message to pr_warn
ceph: add infrastructure for file encryption and decryption
libceph: allow ceph_osdc_new_request to accept a multi-op read
ceph: disable fallocate for encrypted inodes
ceph: disable copy offload on encrypted inodes
ceph: don't use special DIO path for encrypted inodes
ceph: align data in pages in ceph_sync_write
ceph: add read/modify/write to ceph_sync_write
ceph: plumb in decryption during sync reads
ceph: add fscrypt decryption support to ceph_netfs_issue_op
ceph: set i_blkbits to crypto block size for encrypted inodes
ceph: add encryption support to writepage
ceph: fscrypt support for writepages
ceph: report STATX_ATTR_ENCRYPTED on encrypted inodes
Luís Henriques (6):
ceph: don't allow changing layout on encrypted files/directories
ceph: invalidate pages when doing direct/sync writes
ceph: add support for encrypted snapshot names
ceph: add support for handling encrypted snapshot names
ceph: update documentation regarding snapshot naming limitations
ceph: prevent snapshots to be created in encrypted locked directories
Xiubo Li (4):
ceph: add __ceph_get_caps helper support
ceph: add __ceph_sync_read helper support
ceph: add object version support for sync read
ceph: add truncate size handling support for fscrypt
Documentation/filesystems/ceph.rst | 10 + fs/ceph/addr.c | 164 ++++++++-- fs/ceph/caps.c | 143 +++++++-- fs/ceph/crypto.c | 367 +++++++++++++++++++++-- fs/ceph/crypto.h | 118 +++++++- fs/ceph/dir.c | 8 + fs/ceph/file.c | 467 +++++++++++++++++++++++++---- fs/ceph/inode.c | 282 +++++++++++++++-- fs/ceph/ioctl.c | 4 + fs/ceph/mds_client.c | 9 +- fs/ceph/mds_client.h | 2 + fs/ceph/super.c | 6 + fs/ceph/super.h | 11 + include/linux/ceph/osd_client.h | 6 +- include/linux/ceph/rados.h | 4 + net/ceph/osd_client.c | 32 +- 16 files changed, 1454 insertions(+), 179 deletions(-)
Signature · 1 line
2.37.2
PATCH v15 1/29 ceph: don't allow changing layout on encrypted files/directories
From: Luís Henriques <[email protected]>
Encryption is currently only supported on files/directories with layouts where stripe_count=1. Forbid changing layouts when encryption is involved.
--- fs/ceph/ioctl.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ceph/ioctl.c +4 -0
@@ -294,6 +294,10 @@ static long ceph_set_encryption_policy(struct file *file, unsigned long arg)struct inode *inode =file_inode (file);struct ceph_inode_info *ci =ceph_inode (inode); +/* encrypted directories can't have striped layout */ +if (ci->i_layout.stripe_count >1 ) +return -EINVAL ; + ret =vet_mds_for_fscrypt (file);if (ret)return ret;
Signature · 1 line
2.37.2
PATCH v15 2/29 libceph: add CEPH_OSD_OP_ASSERT_VER support
In reply to Jeff Layton...and record the user_version in the reply in a new field in ceph_osd_request, so we can populate the assert_ver appropriately. Shuffle the fields a bit too so that the new field fits in an existing hole on x86_64.
--- include/linux/ceph/osd_client.h | 6 +++++- include/linux/ceph/rados.h | 4 ++++ net/ceph/osd_client.c | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-)
include/linux/ceph/osd_client.h +5 -1
@@ -198,6 +198,9 @@ struct ceph_osd_req_op {u32 src_fadvise_flags;struct ceph_osd_data osd_data; } copy_from; +struct { +u64 ver; + } assert_ver; }; };
@@ -252,6 +255,7 @@ struct ceph_osd_request {struct ceph_osd_client *r_osdc;struct kref r_kref;bool r_mempool; +bool r_linger;/* don't resend on failure */ struct completion r_completion;/* private to osd_client.c */ ceph_osdc_callback_t r_callback;
@@ -264,9 +268,9 @@ struct ceph_osd_request {struct ceph_snap_context *r_snapc;/* for writes */ struct timespec64 r_mtime;/* ditto */ u64 r_data_offset;/* ditto */ -bool r_linger;/* don't resend on failure */ /* internal */ +u64 r_version;/* data version sent in reply */ unsigned long r_stamp;/* jiffies, send or check time */ unsigned long r_start_stamp;/* jiffies */ ktime_t r_start_latency;/* ktime_t */
include/linux/ceph/rados.h +4 -0
@@ -523,6 +523,10 @@ struct ceph_osd_op {struct {__le64 cookie; } __attribute__ ((packed)) notify; +struct { +__le64 unused; +__le64 ver; + } __attribute__ ((packed)) assert_ver;struct {__le64 offset, length;__le64 src_offset;
net/ceph/osd_client.c +5 -0
@@ -1048,6 +1048,10 @@ static u32 osd_req_encode_op(struct ceph_osd_op *dst, dst->copy_from.src_fadvise_flags =cpu_to_le32 (src->copy_from.src_fadvise_flags);break ; +case CEPH_OSD_OP_ASSERT_VER : + dst->assert_ver.unused =cpu_to_le64 (0 ); + dst->assert_ver.ver =cpu_to_le64 (src->assert_ver.ver); +break ;default :pr_err ("unsupported osd opcode %s\n" ,ceph_osd_op_name (src->op));
@@ -3852,6 +3856,7 @@ static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)* one (type of) reply back. */ WARN_ON (!(m.flags &CEPH_OSD_FLAG_ONDISK )); + req->r_version = m.user_version; req->r_result = m.result ?: data_len;finish_request (req);mutex_unlock (&osd->lock);
Signature · 1 line
2.37.2
PATCH v15 3/29 ceph: size handling for encrypted inodes in cap updates
In reply to Jeff LaytonTransmit the rounded-up size as the normal size, and fill out the fscrypt_file field with the real file size.
--- fs/ceph/caps.c | 43 +++++++++++++++++++++++++------------------ fs/ceph/crypto.h | 4 ++++ 2 files changed, 29 insertions(+), 18 deletions(-)
fs/ceph/caps.c +25 -18
@@ -1216,10 +1216,9 @@ struct cap_msg_args {umode_t mode;bool inline_data;bool wake; +bool encrypted;u32 fscrypt_auth_len; -u32 fscrypt_file_len;u8 fscrypt_auth[sizeof (struct ceph_fscrypt_auth )];// for context -u8 fscrypt_file[sizeof (u64)];// for size };/* Marshal up the cap msg to the MDS */
@@ -1254,7 +1253,12 @@ static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg) fc->ino =cpu_to_le64 (arg->ino); fc->snap_follows =cpu_to_le64 (arg->follows); - fc->size =cpu_to_le64 (arg->size); +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +if (arg->encrypted) + fc->size =cpu_to_le64 (round_up (arg->size,CEPH_FSCRYPT_BLOCK_SIZE )); + else +#endif + fc->size =cpu_to_le64 (arg->size); fc->max_size =cpu_to_le64 (arg->max_size);ceph_encode_timespec64 (&fc->mtime, &arg->mtime);ceph_encode_timespec64 (&fc->atime, &arg->atime);
@@ -1314,11 +1318,17 @@ static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)ceph_encode_64 (&p,0 );#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) -/* fscrypt_auth and fscrypt_file (version 12) */ +/* +* fscrypt_auth and fscrypt_file (version 12) +* +* fscrypt_auth holds the crypto context (if any). fscrypt_file +* tracks the real i_size as an __le64 field (and we use a rounded-up +* i_size in * the traditional size field). +*/ ceph_encode_32 (&p, arg->fscrypt_auth_len);ceph_encode_copy (&p, arg->fscrypt_auth, arg->fscrypt_auth_len); -ceph_encode_32 (&p, arg->fscrypt_file_len); -ceph_encode_copy (&p, arg->fscrypt_file, arg->fscrypt_file_len); +ceph_encode_32 (&p,sizeof (__le64)); +ceph_encode_64 (&p, arg->size);#else /* CONFIG_FS_ENCRYPTION */ ceph_encode_32 (&p,0 );ceph_encode_32 (&p,0 );
@@ -1390,7 +1400,6 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap, arg->follows = flushing ? ci->i_head_snapc->seq :0 ; arg->flush_tid = flush_tid; arg->oldest_flush_tid = oldest_flush_tid; - arg->size =i_size_read (inode); ci->i_reported_size = arg->size; arg->max_size = ci->i_wanted_max_size;
@@ -1444,6 +1453,7 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap, } } arg->flags = flags; + arg->encrypted =IS_ENCRYPTED (inode);#if IS_ENABLED (CONFIG_FS_ENCRYPTION )if (ci->fscrypt_auth_len &&WARN_ON_ONCE (ci->fscrypt_auth_len >sizeof (struct ceph_fscrypt_auth ))) {
@@ -1454,21 +1464,21 @@ static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,memcpy (arg->fscrypt_auth, ci->fscrypt_auth,min_t (size_t, ci->fscrypt_auth_len,sizeof (arg->fscrypt_auth))); } -/* FIXME: use this to track "real" size */ - arg->fscrypt_file_len =0 ;#endif /* CONFIG_FS_ENCRYPTION */ } +#if IS_ENABLED (CONFIG_FS_ENCRYPTION )#define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ - 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4) + 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8) -#if IS_ENABLED (CONFIG_FS_ENCRYPTION )static inline int cap_msg_size (struct cap_msg_args *arg) { -return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len + - arg->fscrypt_file_len; +return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len; }#else +#define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ + 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4) +static inline int cap_msg_size (struct cap_msg_args *arg) {return CAP_MSG_FIXED_FIELDS ;
@@ -1547,13 +1557,10 @@ static inline int __send_flush_snap(struct inode *inode, arg.inline_data = capsnap->inline_data; arg.flags =0 ; arg.wake = false; + arg.encrypted =IS_ENCRYPTED (inode); -/* -* No fscrypt_auth changes from a capsnap. It will need -* to update fscrypt_file on size changes (TODO). -*/ +/* No fscrypt_auth changes from a capsnap.*/ arg.fscrypt_auth_len =0 ; - arg.fscrypt_file_len =0 ; msg =ceph_msg_new (CEPH_MSG_CLIENT_CAPS ,cap_msg_size (&arg),GFP_NOFS , false);
fs/ceph/crypto.h +4 -0
@@ -9,6 +9,10 @@#include <crypto/sha2.h> #include <linux/fscrypt.h> +#define CEPH_FSCRYPT_BLOCK_SHIFT 12 +#define CEPH_FSCRYPT_BLOCK_SIZE (_AC(1, UL) << CEPH_FSCRYPT_BLOCK_SHIFT) +#define CEPH_FSCRYPT_BLOCK_MASK (~(CEPH_FSCRYPT_BLOCK_SIZE-1)) +struct ceph_fs_client ;struct ceph_acl_sec_ctx ;struct ceph_mds_request ;
Signature · 1 line
2.37.2
PATCH v15 4/29 ceph: fscrypt_file field handling in MClientRequest messages
In reply to Jeff LaytonFor encrypted inodes, transmit a rounded-up size to the MDS as the normal file size and send the real inode size in fscrypt_file field.
Also, fix up creates and truncates to also transmit fscrypt_file.
--- fs/ceph/dir.c | 3 +++ fs/ceph/file.c | 1 + fs/ceph/inode.c | 18 ++++++++++++++++-- fs/ceph/mds_client.c | 9 ++++++++- fs/ceph/mds_client.h | 2 ++ 5 files changed, 30 insertions(+), 3 deletions(-)
fs/ceph/dir.c +3 -0
@@ -914,6 +914,9 @@ static int ceph_mknod(struct user_namespace *mnt_userns, struct inode *dir, goto out_req; } +if (S_ISREG (mode) &&IS_ENCRYPTED (dir)) +set_bit (CEPH_MDS_R_FSCRYPT_FILE , &req->r_req_flags); + req->r_dentry =dget (dentry); req->r_num_caps =2 ; req->r_parent = dir;
fs/ceph/file.c +1 -0
@@ -785,6 +785,7 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry, req->r_parent = dir;ihold (dir);if (IS_ENCRYPTED (dir)) { +set_bit (CEPH_MDS_R_FSCRYPT_FILE , &req->r_req_flags);if (!fscrypt_has_encryption_key (dir)) {spin_lock (&dentry->d_lock); dentry->d_flags |=DCACHE_NOKEY_NAME ;
fs/ceph/inode.c +16 -2
@@ -2390,11 +2390,25 @@ int __ceph_setattr(struct inode *inode, struct iattr *attr, struct ceph_iattr *c } }else if ((issued &CEPH_CAP_FILE_SHARED ) ==0 || attr->ia_size != isize) { - req->r_args.setattr.size =cpu_to_le64 (attr->ia_size); - req->r_args.setattr.old_size =cpu_to_le64 (isize); mask |=CEPH_SETATTR_SIZE ; release |=CEPH_CAP_FILE_SHARED |CEPH_CAP_FILE_EXCL |CEPH_CAP_FILE_RD |CEPH_CAP_FILE_WR ; +if (IS_ENCRYPTED (inode) && attr->ia_size) { +set_bit (CEPH_MDS_R_FSCRYPT_FILE , &req->r_req_flags); + mask |=CEPH_SETATTR_FSCRYPT_FILE ; + req->r_args.setattr.size = +cpu_to_le64 (round_up (attr->ia_size, +CEPH_FSCRYPT_BLOCK_SIZE )); + req->r_args.setattr.old_size = +cpu_to_le64 (round_up (isize, +CEPH_FSCRYPT_BLOCK_SIZE )); + req->r_fscrypt_file = attr->ia_size; +/* FIXME: client must zero out any partial blocks! */ + }else { + req->r_args.setattr.size =cpu_to_le64 (attr->ia_size); + req->r_args.setattr.old_size =cpu_to_le64 (isize); + req->r_fscrypt_file =0 ; + } } }if (ia_valid &ATTR_MTIME ) {
fs/ceph/mds_client.c +8 -1
@@ -2830,7 +2830,12 @@ static void encode_mclientrequest_tail(void **p, const struct ceph_mds_request * }else {ceph_encode_32 (p,0 ); } -ceph_encode_32 (p,0 );// fscrypt_file for now +if (test_bit (CEPH_MDS_R_FSCRYPT_FILE , &req->r_req_flags)) { +ceph_encode_32 (p,sizeof (__le64)); +ceph_encode_64 (p, req->r_fscrypt_file); + }else { +ceph_encode_32 (p,0 ); + } }/*
@@ -2916,6 +2921,8 @@ static struct ceph_msg *create_request_message(struct ceph_mds_session *session,/* fscrypt_file */ len +=sizeof (u32); +if (test_bit (CEPH_MDS_R_FSCRYPT_FILE , &req->r_req_flags)) + len +=sizeof (__le64); msg =ceph_msg_new2 (CEPH_MSG_CLIENT_REQUEST , len,1 ,GFP_NOFS , false);if (!msg) {
fs/ceph/mds_client.h +2 -0
@@ -282,6 +282,7 @@ struct ceph_mds_request {#define CEPH_MDS_R_DID_PREPOPULATE (6)/* prepopulated readdir */ #define CEPH_MDS_R_PARENT_LOCKED (7)/* is r_parent->i_rwsem wlocked? */ #define CEPH_MDS_R_ASYNC (8)/* async request */ +#define CEPH_MDS_R_FSCRYPT_FILE (9)/* must marshal fscrypt_file field */ unsigned long r_req_flags;struct mutex r_fill_mutex;
@@ -289,6 +290,7 @@ struct ceph_mds_request {union ceph_mds_request_args r_args;struct ceph_fscrypt_auth *r_fscrypt_auth; +u64 r_fscrypt_file;u8 *r_altname;/* fscrypt binary crypttext for long filenames */ u32 r_altname_len;/* length of r_altname */
Signature · 1 line
2.37.2
PATCH v15 5/29 ceph: get file size from fscrypt_file when present in inode traces
In reply to Jeff LaytonWhen we get an inode trace from the MDS, grab the fscrypt_file field if the inode is encrypted, and use it to populate the i_size field instead of the regular inode size field.
--- fs/ceph/inode.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
fs/ceph/inode.c +13 -2
@@ -1036,6 +1036,7 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,if (new_version || (new_issued & (CEPH_CAP_ANY_FILE_RD |CEPH_CAP_ANY_FILE_WR ))) { +u64 size =le64_to_cpu (info->size);s64 old_pool = ci->i_layout.pool_id;struct ceph_string *old_ns;
@@ -1049,11 +1050,21 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page, pool_ns = old_ns; +if (IS_ENCRYPTED (inode) && size && (iinfo->fscrypt_file_len ==sizeof (__le64))) { +u64 fsize =__le64_to_cpu (*(__le64 *)iinfo->fscrypt_file); + +if (size ==round_up (fsize,CEPH_FSCRYPT_BLOCK_SIZE )) { + size = fsize; + }else { +pr_warn ("fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n" , + info->size, size); + } + } + queue_trunc =ceph_fill_file_size (inode, issued,le32_to_cpu (info->truncate_seq),le64_to_cpu (info->truncate_size), -le64_to_cpu (info->size), - info_caps); + size, info_caps);/* only update max_size on auth cap */ if ((info->cap.flags &CEPH_CAP_FLAG_AUTH ) && ci->i_max_size !=le64_to_cpu (info->max_size)) {
Signature · 1 line
2.37.2
PATCH v15 6/29 ceph: handle fscrypt fields in cap messages from MDS
In reply to Jeff LaytonHandle the new fscrypt_file and fscrypt_auth fields in cap messages. Use them to populate new fields in cap_extra_info and update the inode with those values.
--- fs/ceph/caps.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-)
fs/ceph/caps.c +76 -2
@@ -3394,6 +3394,9 @@ struct cap_extra_info {/* currently issued */ int issued;struct timespec64 btime; +u8 *fscrypt_auth; +u32 fscrypt_auth_len; +u64 fscrypt_file_size; };/*
@@ -3426,6 +3429,14 @@ static void handle_cap_grant(struct inode *inode,bool deleted_inode = false;bool fill_inline = false; +/* +* If there is at least one crypto block then we'll trust fscrypt_file_size. +* If the real length of the file is 0, then ignore it (it has probably been +* truncated down to 0 by the MDS). +*/ +if (IS_ENCRYPTED (inode) && size) + size = extra_info->fscrypt_file_size; +dout ("handle_cap_grant inode %p cap %p mds%d seq %d %s\n" , inode, cap, session->s_mds, seq,ceph_cap_string (newcaps)); dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
@@ -3492,6 +3503,10 @@ static void handle_cap_grant(struct inode *inode,dout ("%p mode 0%o uid.gid %d.%d\n" , inode, inode->i_mode,from_kuid (&init_user_ns, inode->i_uid),from_kgid (&init_user_ns, inode->i_gid)); + +WARN_ON_ONCE (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len || +memcmp (ci->fscrypt_auth, extra_info->fscrypt_auth, + ci->fscrypt_auth_len)); } if ((newcaps & CEPH_CAP_LINK_SHARED) &&
@@ -3904,7 +3919,8 @@ static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,*/ static bool handle_cap_trunc (struct inode *inode,struct ceph_mds_caps *trunc, -struct ceph_mds_session *session) +struct ceph_mds_session *session, +struct cap_extra_info *extra_info) {struct ceph_inode_info *ci =ceph_inode (inode);int mds = session->s_mds;
@@ -3921,6 +3937,14 @@ static bool handle_cap_trunc(struct inode *inode, issued |= implemented | dirty; +/* +* If there is at least one crypto block then we'll trust fscrypt_file_size. +* If the real length of the file is 0, then ignore it (it has probably been +* truncated down to 0 by the MDS). +*/ +if (IS_ENCRYPTED (inode) && size) + size = extra_info->fscrypt_file_size; +dout ("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n" , inode, mds, seq, truncate_size, truncate_seq); queue_trunc = ceph_fill_file_size(inode, issued,
@@ -4143,6 +4167,49 @@ static void handle_cap_import(struct ceph_mds_client *mdsc, *target_cap = cap; } +#ifdef CONFIG_FS_ENCRYPTION +static int parse_fscrypt_fields (void **p,void *end,struct cap_extra_info *extra) +{ +u32 len; + +ceph_decode_32_safe (p, end, extra->fscrypt_auth_len, bad); +if (extra->fscrypt_auth_len) { +ceph_decode_need (p, end, extra->fscrypt_auth_len, bad); + extra->fscrypt_auth =kmalloc (extra->fscrypt_auth_len,GFP_KERNEL ); +if (!extra->fscrypt_auth) +return -ENOMEM ; +ceph_decode_copy_safe (p, end, extra->fscrypt_auth, + extra->fscrypt_auth_len, bad); + } + +ceph_decode_32_safe (p, end, len, bad); +if (len >=sizeof (u64)) { +ceph_decode_64_safe (p, end, extra->fscrypt_file_size, bad); + len -=sizeof (u64); + } +ceph_decode_skip_n (p, end, len, bad); +return 0 ; +bad: +return -EIO ; +} +#else +static int parse_fscrypt_fields (void **p,void *end,struct cap_extra_info *extra) +{ +u32 len; + +/* Don't care about these fields unless we're encryption-capable */ +ceph_decode_32_safe (p, end, len, bad); +if (len) +ceph_decode_skip_n (p, end, len, bad); +ceph_decode_32_safe (p, end, len, bad); +if (len) +ceph_decode_skip_n (p, end, len, bad); +return 0 ; +bad: +return -EIO ; +} +#endif +/* * Handle a caps message from the MDS. *
@@ -4261,6 +4328,11 @@ void ceph_handle_caps(struct ceph_mds_session *session,ceph_decode_64_safe (&p, end, extra_info.nsubdirs, bad); } +if (msg_version >=12 ) { +if (parse_fscrypt_fields (&p, end, &extra_info)) + goto bad; + } +/* lookup ino */ inode =ceph_find_inode (mdsc->fsc->sb, vino); dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
@@ -4357,7 +4429,8 @@ void ceph_handle_caps(struct ceph_mds_session *session,break ;case CEPH_CAP_OP_TRUNC : - queue_trunc =handle_cap_trunc (inode, h, session); + queue_trunc =handle_cap_trunc (inode, h, session, + &extra_info);spin_unlock (&ci->i_ceph_lock);if (queue_trunc)ceph_queue_vmtruncate (inode);
@@ -4375,6 +4448,7 @@ void ceph_handle_caps(struct ceph_mds_session *session,iput (inode); out:ceph_put_string (extra_info.pool_ns); +kfree (extra_info.fscrypt_auth);return ; flush_cap_releases:
Signature · 1 line
2.37.2
PATCH v15 7/29 ceph: update WARN_ON message to pr_warn
In reply to Jeff LaytonGive some more helpful info
--- fs/ceph/caps.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
fs/ceph/caps.c +7 -4
@@ -3503,10 +3503,13 @@ static void handle_cap_grant(struct inode *inode,dout ("%p mode 0%o uid.gid %d.%d\n" , inode, inode->i_mode,from_kuid (&init_user_ns, inode->i_uid),from_kgid (&init_user_ns, inode->i_gid)); - -WARN_ON_ONCE (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len || -memcmp (ci->fscrypt_auth, extra_info->fscrypt_auth, - ci->fscrypt_auth_len)); +#if IS_ENABLED (CONFIG_FS_ENCRYPTION ) +if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len || +memcmp (ci->fscrypt_auth, extra_info->fscrypt_auth, + ci->fscrypt_auth_len)) +pr_warn_ratelimited ("%s: cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n" , + __func__, ci->fscrypt_auth_len, extra_info->fscrypt_auth_len); +#endif } if ((newcaps & CEPH_CAP_LINK_SHARED) &&
Signature · 1 line
2.37.2
PATCH v15 8/29 ceph: add __ceph_get_caps helper support
In reply to Jeff LaytonFrom: Xiubo Li <[email protected]>
Break out the guts of ceph_get_caps into a helper that takes an inode and ceph_file_info instead of a file pointer.
--- fs/ceph/caps.c | 19 +++++++++++++------ fs/ceph/super.h | 2 ++ 2 files changed, 15 insertions(+), 6 deletions(-)
fs/ceph/caps.c +13 -6
@@ -2977,10 +2977,9 @@ int ceph_try_get_caps(struct inode *inode, int need, int want,* due to a small max_size, make sure we check_max_size (and possibly * ask the mds) so we don't get hung up indefinitely. */ -int ceph_get_caps (struct file *filp,int need,int want,loff_t endoff,int *got) +int __ceph_get_caps (struct inode *inode,struct ceph_file_info *fi,int need, +int want,loff_t endoff,int *got) { -struct ceph_file_info *fi = filp->private_data; -struct inode *inode =file_inode (filp);struct ceph_inode_info *ci =ceph_inode (inode);struct ceph_fs_client *fsc =ceph_inode_to_client (inode);int ret, _got, flags;
@@ -2989,7 +2988,7 @@ int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, int *gotif (ret <0 )return ret; -if ((fi->fmode &CEPH_FILE_MODE_WR ) && +if (fi && (fi->fmode &CEPH_FILE_MODE_WR ) && fi->filp_gen !=READ_ONCE (fsc->filp_gen))return -EBADF ;
@@ -2997,7 +2996,7 @@ int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, int *gotwhile (true) { flags &=CEPH_FILE_MODE_MASK ; -if (atomic_read (&fi->num_locks)) +if (fi &&atomic_read (&fi->num_locks)) flags |=CHECK_FILELOCK ; _got =0 ; ret = try_get_cap_refs(inode, need, want, endoff,
@@ -3042,7 +3041,7 @@ int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, int *gotcontinue ; } -if ((fi->fmode &CEPH_FILE_MODE_WR ) && +if (fi && (fi->fmode &CEPH_FILE_MODE_WR ) && fi->filp_gen !=READ_ONCE (fsc->filp_gen)) {if (ret >=0 && _got)ceph_put_cap_refs (ci, _got);
@@ -3105,6 +3104,14 @@ int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, int *gotreturn 0 ; } +int ceph_get_caps (struct file *filp,int need,int want,loff_t endoff,int *got) +{ +struct ceph_file_info *fi = filp->private_data; +struct inode *inode =file_inode (filp); + +return __ceph_get_caps (inode, fi, need, want, endoff, got); +} +/* * Take cap refs. Caller must already know we hold at least one ref * on the caps in question or we don't know this is safe.
fs/ceph/super.h +2 -0
@@ -1239,6 +1239,8 @@ extern int ceph_encode_dentry_release(void **p, struct dentry *dn,struct inode *dir, int mds, int drop, int unless); +extern int __ceph_get_caps (struct inode *inode,struct ceph_file_info *fi, +int need,int want,loff_t endoff,int *got);extern int ceph_get_caps (struct file *filp,int need,int want,loff_t endoff,int *got); extern int ceph_try_get_caps(struct inode *inode,
Signature · 1 line
2.37.2
PATCH v15 9/29 ceph: add __ceph_sync_read helper support
In reply to Jeff LaytonFrom: Xiubo Li <[email protected]>
Turn the guts of ceph_sync_read into a new helper that takes an inode and an offset instead of a kiocb struct, and make ceph_sync_read call the helper as a wrapper.
--- fs/ceph/file.c | 33 +++++++++++++++++++++------------ fs/ceph/super.h | 2 ++ 2 files changed, 23 insertions(+), 12 deletions(-)
fs/ceph/file.c +21 -12
@@ -951,22 +951,19 @@ enum {* If we get a short result from the OSD, check against i_size; we need to * only return a short read to the caller if we hit EOF. */ -static ssize_t ceph_sync_read (struct kiocb *iocb,struct iov_iter *to, -int *retry_op) +ssize_t __ceph_sync_read (struct inode *inode,loff_t *ki_pos, +struct iov_iter *to,int *retry_op) { -struct file *file = iocb->ki_filp; -struct inode *inode =file_inode (file);struct ceph_inode_info *ci =ceph_inode (inode);struct ceph_fs_client *fsc =ceph_inode_to_client (inode);struct ceph_osd_client *osdc = &fsc->client->osdc;ssize_t ret; -u64 off = iocb->ki_pos; +u64 off = *ki_pos;u64 len =iov_iter_count (to);u64 i_size =i_size_read (inode);bool sparse =ceph_test_mount_opt (fsc,SPARSEREAD ); -dout ("sync_read on file %p %llu~%u %s\n" , file, off, (unsigned )len, - (file->f_flags &O_DIRECT ) ?"O_DIRECT" :"" ); +dout ("sync_read on inode %p %llx~%llx\n" , inode, *ki_pos, len);if (!len)return 0 ;
@@ -1084,14 +1081,14 @@ static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,break ; } -if (off > iocb->ki_pos) { +if (off > *ki_pos) {if (off >= i_size) { *retry_op =CHECK_EOF ; - ret = i_size - iocb->ki_pos; - iocb->ki_pos = i_size; + ret = i_size - *ki_pos; + *ki_pos = i_size; }else { - ret = off - iocb->ki_pos; - iocb->ki_pos = off; + ret = off - *ki_pos; + *ki_pos = off; } }
@@ -1099,6 +1096,18 @@ static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,return ret; } +static ssize_t ceph_sync_read (struct kiocb *iocb,struct iov_iter *to, +int *retry_op) +{ +struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file); + +dout ("sync_read on file %p %llx~%zx %s\n" , file, iocb->ki_pos, +iov_iter_count (to), (file->f_flags &O_DIRECT ) ?"O_DIRECT" :"" ); + +return __ceph_sync_read (inode, &iocb->ki_pos, to, retry_op); +} +struct ceph_aio_request {struct kiocb *iocb;size_t total_len;
fs/ceph/super.h +2 -0
@@ -1276,6 +1276,8 @@ extern int ceph_renew_caps(struct inode *inode, int fmode);extern int ceph_open (struct inode *inode,struct file *file);extern int ceph_atomic_open (struct inode *dir,struct dentry *dentry,struct file *file,unsigned flags,umode_t mode); +extern ssize_t __ceph_sync_read (struct inode *inode,loff_t *ki_pos, +struct iov_iter *to,int *retry_op);extern int ceph_release (struct inode *inode,struct file *filp);extern void ceph_fill_inline_data (struct inode *inode,struct page *locked_page,char *data,size_t len);
Signature · 1 line
2.37.2
PATCH v15 10/29 ceph: add object version support for sync read
In reply to Jeff LaytonFrom: Xiubo Li <[email protected]>
Always return the last object's version.
--- fs/ceph/file.c | 12 ++++++++++-- fs/ceph/super.h | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-)
fs/ceph/file.c +10 -2
@@ -952,7 +952,8 @@ enum {* only return a short read to the caller if we hit EOF. */ ssize_t __ceph_sync_read (struct inode *inode,loff_t *ki_pos, -struct iov_iter *to,int *retry_op) +struct iov_iter *to,int *retry_op, +u64 *last_objver) {struct ceph_inode_info *ci =ceph_inode (inode);struct ceph_fs_client *fsc =ceph_inode_to_client (inode);
@@ -962,6 +963,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,u64 len =iov_iter_count (to);u64 i_size =i_size_read (inode);bool sparse =ceph_test_mount_opt (fsc,SPARSEREAD ); +u64 objver =0 ;dout ("sync_read on inode %p %llx~%llx\n" , inode, *ki_pos, len);
@@ -1031,6 +1033,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, req->r_end_latency, len, ret); +if (ret >0 ) + objver = req->r_version; + i_size =i_size_read (inode);dout ("sync_read %llu~%llu got %zd i_size %llu%s\n" , off, len, ret, i_size, (more ?" MORE" :"" ));
@@ -1092,6 +1097,9 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, } } +if (last_objver && ret >0 ) + *last_objver = objver; +dout ("sync_read result %zd retry_op %d\n" , ret, *retry_op);return ret; }
@@ -1105,7 +1113,7 @@ static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,dout ("sync_read on file %p %llx~%zx %s\n" , file, iocb->ki_pos,iov_iter_count (to), (file->f_flags &O_DIRECT ) ?"O_DIRECT" :"" ); -return __ceph_sync_read (inode, &iocb->ki_pos, to, retry_op); +return __ceph_sync_read (inode, &iocb->ki_pos, to, retry_op,NULL ); }struct ceph_aio_request {
fs/ceph/super.h +2 -1
@@ -1277,7 +1277,8 @@ extern int ceph_open(struct inode *inode, struct file *file);extern int ceph_atomic_open (struct inode *dir,struct dentry *dentry,struct file *file,unsigned flags,umode_t mode);extern ssize_t __ceph_sync_read (struct inode *inode,loff_t *ki_pos, -struct iov_iter *to,int *retry_op); +struct iov_iter *to,int *retry_op, +u64 *last_objver);extern int ceph_release (struct inode *inode,struct file *filp);extern void ceph_fill_inline_data (struct inode *inode,struct page *locked_page,char *data,size_t len);
Signature · 1 line
2.37.2
PATCH v15 11/29 ceph: add infrastructure for file encryption and decryption
In reply to Jeff Layton...and allow test_dummy_encryption to bypass content encryption if mounted with test_dummy_encryption=clear.
--- fs/ceph/crypto.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++ fs/ceph/crypto.h | 71 +++++++++++++++++++ fs/ceph/super.c | 6 ++ fs/ceph/super.h | 1 + 4 files changed, 255 insertions(+)
fs/ceph/crypto.c +177 -0
@@ -9,6 +9,7 @@#include <linux/ceph/ceph_debug.h> #include <linux/xattr.h> #include <linux/fscrypt.h> +#include <linux/ceph/striper.h> #include "super.h" #include "mds_client.h"
@@ -323,3 +324,179 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,fscrypt_fname_free_buffer (&_tname);return ret; } + +int ceph_fscrypt_decrypt_block_inplace (const struct inode *inode, +struct page *page,unsigned int len, +unsigned int offs,u64 lblk_num) +{ +struct ceph_mount_options *opt =ceph_inode_to_client (inode)->mount_options; + +if (opt->flags &CEPH_MOUNT_OPT_DUMMY_ENC_CLEAR ) +return 0 ; + +dout ("%s: len %u offs %u blk %llu\n" , __func__, len, offs, lblk_num); +return fscrypt_decrypt_block_inplace (inode, page, len, offs, lblk_num); +} + +int ceph_fscrypt_encrypt_block_inplace (const struct inode *inode, +struct page *page,unsigned int len, +unsigned int offs,u64 lblk_num,gfp_t gfp_flags) +{ +struct ceph_mount_options *opt =ceph_inode_to_client (inode)->mount_options; + +if (opt->flags &CEPH_MOUNT_OPT_DUMMY_ENC_CLEAR ) +return 0 ; + +dout ("%s: len %u offs %u blk %llu\n" , __func__, len, offs, lblk_num); +return fscrypt_encrypt_block_inplace (inode, page, len, offs, lblk_num, gfp_flags); +} + +/** +* ceph_fscrypt_decrypt_pages - decrypt an array of pages +* @inode: pointer to inode associated with these pages +* @page: pointer to page array +* @off: offset into the file that the read data starts +* @len: max length to decrypt +* +* Decrypt an array of fscrypt'ed pages and return the amount of +* data decrypted. Any data in the page prior to the start of the +* first complete block in the read is ignored. Any incomplete +* crypto blocks at the end of the array are ignored (and should +* probably be zeroed by the caller). +* +* Returns the length of the decrypted data or a negative errno. +*/ +int ceph_fscrypt_decrypt_pages (struct inode *inode,struct page **page,u64 off,int len) +{ +int i, num_blocks; +u64 baseblk = off >>CEPH_FSCRYPT_BLOCK_SHIFT ; +int ret =0 ; + +/* +* We can't deal with partial blocks on an encrypted file, so mask off +* the last bit. +*/ + num_blocks =ceph_fscrypt_blocks (off, len &CEPH_FSCRYPT_BLOCK_MASK ); + +/* Decrypt each block */ +for (i =0 ; i < num_blocks; ++i) { +int blkoff = i <<CEPH_FSCRYPT_BLOCK_SHIFT ; +int pgidx = blkoff >>PAGE_SHIFT ; +unsigned int pgoffs =offset_in_page (blkoff); +int fret; + + fret =ceph_fscrypt_decrypt_block_inplace (inode, page[pgidx], +CEPH_FSCRYPT_BLOCK_SIZE , pgoffs, + baseblk + i); +if (fret <0 ) { +if (ret ==0 ) + ret = fret; +break ; + } + ret +=CEPH_FSCRYPT_BLOCK_SIZE ; + } +return ret; +} + +/** +* ceph_fscrypt_decrypt_extents: decrypt received extents in given buffer +* @inode: inode associated with pages being decrypted +* @page: pointer to page array +* @off: offset into the file that the data in page[0] starts +* @map: pointer to extent array +* @ext_cnt: length of extent array +* +* Given an extent map and a page array, decrypt the received data in-place, +* skipping holes. Returns the offset into buffer of end of last decrypted +* block. +*/ +int ceph_fscrypt_decrypt_extents (struct inode *inode,struct page **page,u64 off, +struct ceph_sparse_extent *map,u32 ext_cnt) +{ +int i, ret =0 ; +struct ceph_inode_info *ci =ceph_inode (inode); +u64 objno, objoff; +u32 xlen; + +/* Nothing to do for empty array */ +if (ext_cnt ==0 ) { +dout ("%s: empty array, ret 0\n" , __func__); +return 0 ; + } + +ceph_calc_file_object_mapping (&ci->i_layout, off, map[0 ].len, + &objno, &objoff, &xlen); + +for (i =0 ; i < ext_cnt; ++i) { +struct ceph_sparse_extent *ext = &map[i]; +int pgsoff = ext->off - objoff; +int pgidx = pgsoff >>PAGE_SHIFT ; +int fret; + +if ((ext->off | ext->len) & ~CEPH_FSCRYPT_BLOCK_MASK ) { +pr_warn ("%s: bad encrypted sparse extent idx %d off %llx len %llx\n" , + __func__, i, ext->off, ext->len); +return -EIO ; + } + fret =ceph_fscrypt_decrypt_pages (inode, &page[pgidx], + off + pgsoff, ext->len); +dout ("%s: [%d] 0x%llx~0x%llx fret %d\n" , __func__, i, + ext->off, ext->len, fret); +if (fret <0 ) { +if (ret ==0 ) + ret = fret; +break ; + } + ret = pgsoff + fret; + } +dout ("%s: ret %d\n" , __func__, ret); +return ret; +} + +/** +* ceph_fscrypt_encrypt_pages - encrypt an array of pages +* @inode: pointer to inode associated with these pages +* @page: pointer to page array +* @off: offset into the file that the data starts +* @len: max length to encrypt +* @gfp: gfp flags to use for allocation +* +* Decrypt an array of cleartext pages and return the amount of +* data encrypted. Any data in the page prior to the start of the +* first complete block in the read is ignored. Any incomplete +* crypto blocks at the end of the array are ignored. +* +* Returns the length of the encrypted data or a negative errno. +*/ +int ceph_fscrypt_encrypt_pages (struct inode *inode,struct page **page,u64 off, +int len,gfp_t gfp) +{ +int i, num_blocks; +u64 baseblk = off >>CEPH_FSCRYPT_BLOCK_SHIFT ; +int ret =0 ; + +/* +* We can't deal with partial blocks on an encrypted file, so mask off +* the last bit. +*/ + num_blocks =ceph_fscrypt_blocks (off, len &CEPH_FSCRYPT_BLOCK_MASK ); + +/* Encrypt each block */ +for (i =0 ; i < num_blocks; ++i) { +int blkoff = i <<CEPH_FSCRYPT_BLOCK_SHIFT ; +int pgidx = blkoff >>PAGE_SHIFT ; +unsigned int pgoffs =offset_in_page (blkoff); +int fret; + + fret =ceph_fscrypt_encrypt_block_inplace (inode, page[pgidx], +CEPH_FSCRYPT_BLOCK_SIZE , pgoffs, + baseblk + i, gfp); +if (fret <0 ) { +if (ret ==0 ) + ret = fret; +break ; + } + ret +=CEPH_FSCRYPT_BLOCK_SIZE ; + } +return ret; +}
fs/ceph/crypto.h +71 -0
83 lines not on this page · raw message
fs/ceph/super.c +6 -0
@@ -591,6 +591,12 @@ static int ceph_parse_mount_param(struct fs_context *fc,break ;case Opt_test_dummy_encryption:#ifdef CONFIG_FS_ENCRYPTION +/* HACK: allow for cleartext "encryption" in files for testing */ +if (param->string && !strcmp (param->string,"clear" )) { + fsopt->flags |=CEPH_MOUNT_OPT_DUMMY_ENC_CLEAR ; +kfree (param->string); + param->string =NULL ; + }fscrypt_free_dummy_policy (&fsopt->dummy_enc_policy); ret =fscrypt_parse_test_dummy_encryption (param, &fsopt->dummy_enc_policy);
fs/ceph/super.h +1 -0
@@ -44,6 +44,7 @@#define CEPH_MOUNT_OPT_ASYNC_DIROPS (1<<15)/* allow async directory ops */ #define CEPH_MOUNT_OPT_NOPAGECACHE (1<<16)/* bypass pagecache altogether */ #define CEPH_MOUNT_OPT_SPARSEREAD (1<<17)/* always do sparse reads */ +#define CEPH_MOUNT_OPT_DUMMY_ENC_CLEAR (1<<18)/* don't actually encrypt content */ #define CEPH_MOUNT_OPT_DEFAULT \ (CEPH_MOUNT_OPT_DCACHE | \
Signature · 1 line
2.37.2
PATCH v15 12/29 ceph: add truncate size handling support for fscrypt
In reply to Jeff LaytonFrom: Xiubo Li <[email protected]>
This will transfer the encrypted last block contents to the MDS along with the truncate request only when the new size is smaller and not aligned to the fscrypt BLOCK size. When the last block is located in the file hole, the truncate request will only contain the header.
The MDS could fail to do the truncate if there has another client or process has already updated the RADOS object which contains the last block, and will return -EAGAIN, then the kclient needs to retry it. The RMW will take around 50ms, and will let it retry 20 times for now.
--- fs/ceph/crypto.h | 21 +++++ fs/ceph/inode.c | 198 +++++++++++++++++++++++++++++++++++++++++++++-- fs/ceph/super.h | 5 ++ 3 files changed, 217 insertions(+), 7 deletions(-)
fs/ceph/crypto.h +21 -0
@@ -26,6 +26,27 @@ struct ceph_fname {bool no_copy; }; +/* +* Header for the crypted file when truncating the size, this +* will be sent to MDS, and the MDS will update the encrypted +* last block and then truncate the size. +*/ +struct ceph_fscrypt_truncate_size_header { +__u8 ver; +__u8 compat; + +/* +* It will be sizeof(assert_ver + file_offset + block_size) +* if the last block is empty when it's located in a file +* hole. Or the data_len will plus CEPH_FSCRYPT_BLOCK_SIZE. +*/ +__le32 data_len; + +__le64 change_attr; +__le64 file_offset; +__le32 block_size; +} __packed; +struct ceph_fscrypt_auth {__le32 cfa_version;__le32 cfa_blob_len;
fs/ceph/inode.c +191 -7
274 lines not on this page · raw message
fs/ceph/super.h +5 -0
@@ -416,6 +416,11 @@ struct ceph_inode_info {u32 i_truncate_seq;/* last truncate to smaller size */ u64 i_truncate_size;/* and the size we last truncated down to */ int i_truncate_pending;/* still need to call vmtruncate */ +/* +* For none fscrypt case it equals to i_truncate_size or it will +* equals to fscrypt_file_size +*/ +u64 i_truncate_pagecache_size;u64 i_max_size;/* max file size authorized by mds */ u64 i_reported_size;/* (max_)size reported to or requested of mds */
Signature · 1 line
2.37.2
PATCH v15 13/29 libceph: allow ceph_osdc_new_request to accept a multi-op read
In reply to Jeff LaytonCurrently we have some special-casing for multi-op writes, but in the case of a read, we can't really handle it. All of the current multi-op callers call it with CEPH_OSD_FLAG_WRITE set.
Have ceph_osdc_new_request check for CEPH_OSD_FLAG_READ and if it's set, allocate multiple reply ops instead of multiple request ops. If neither flag is set, return -EINVAL.
--- net/ceph/osd_client.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-)
net/ceph/osd_client.c +21 -6
@@ -1136,15 +1136,30 @@ struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,if (flags &CEPH_OSD_FLAG_WRITE ) req->r_data_offset = off; -if (num_ops >1 ) +if (num_ops >1 ) { +int num_req_ops, num_rep_ops; +/* -* This is a special case for ceph_writepages_start(), but it -* also covers ceph_uninline_data(). If more multi-op request -* use cases emerge, we will need a separate helper. +* If this is a multi-op write request, assume that we'll need +* request ops. If it's a multi-op read then assume we'll need +* reply ops. Anything else and call it -EINVAL. */ - r =__ceph_osdc_alloc_messages (req,GFP_NOFS , num_ops,0 ); -else +if (flags &CEPH_OSD_FLAG_WRITE ) { + num_req_ops = num_ops; + num_rep_ops =0 ; + }else if (flags &CEPH_OSD_FLAG_READ ) { + num_req_ops =0 ; + num_rep_ops = num_ops; + }else { + r = -EINVAL ; + goto fail; + } + + r =__ceph_osdc_alloc_messages (req,GFP_NOFS , num_req_ops, + num_rep_ops); + }else { r =ceph_osdc_alloc_messages (req,GFP_NOFS ); + }if (r) goto fail;
Signature · 1 line
2.37.2
PATCH v15 14/29 ceph: disable fallocate for encrypted inodes
In reply to Jeff Layton...hopefully, just for now.
--- fs/ceph/file.c | 3 +++ 1 file changed, 3 insertions(+)
fs/ceph/file.c +3 -0
@@ -2183,6 +2183,9 @@ static long ceph_fallocate(struct file *file, int mode,if (!S_ISREG (inode->i_mode))return -EOPNOTSUPP ; +if (IS_ENCRYPTED (inode)) +return -EOPNOTSUPP ; + prealloc_cf =ceph_alloc_cap_flush ();if (!prealloc_cf)return -ENOMEM ;
Signature · 1 line
2.37.2
PATCH v15 15/29 ceph: disable copy offload on encrypted inodes
In reply to Jeff LaytonIf we have an encrypted inode, then the client will need to re-encrypt the contents of the new object. Disable copy offload to or from encrypted inodes.
--- fs/ceph/file.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ceph/file.c +4 -0
@@ -2502,6 +2502,10 @@ static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,return -EOPNOTSUPP ; } +/* Every encrypted inode gets its own key, so we can't offload them */ +if (IS_ENCRYPTED (src_inode) ||IS_ENCRYPTED (dst_inode)) +return -EOPNOTSUPP ; +if (len < src_ci->i_layout.object_size)return -EOPNOTSUPP ;/* no remote copy will be done */
Signature · 1 line
2.37.2
PATCH v15 16/29 ceph: don't use special DIO path for encrypted inodes
In reply to Jeff LaytonEventually I want to merge the synchronous and direct read codepaths, possibly via new netfs infrastructure. For now, the direct path is not crypto-enabled, so use the sync read/write paths instead.
--- fs/ceph/file.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/ceph/file.c +4 -2
@@ -1729,7 +1729,9 @@ static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to) ceph_cap_string(got));if (!ceph_has_inline_data (ci)) { -if (!retry_op && (iocb->ki_flags &IOCB_DIRECT )) { +if (!retry_op && + (iocb->ki_flags &IOCB_DIRECT ) && + !IS_ENCRYPTED (inode)) { ret =ceph_direct_read_write (iocb, to,NULL ,NULL );if (ret >=0 && ret < len)
@@ -1955,7 +1957,7 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)/* we might need to revert back to that point */ data = *from; -if (iocb->ki_flags &IOCB_DIRECT ) +if ((iocb->ki_flags &IOCB_DIRECT ) && !IS_ENCRYPTED (inode)) written =ceph_direct_read_write (iocb, &data, snapc, &prealloc_cf);else
Signature · 1 line
2.37.2
PATCH v15 17/29 ceph: align data in pages in ceph_sync_write
In reply to Jeff LaytonEncrypted files will need to be dealt with in block-sized chunks and once we do that, the way that ceph_sync_write aligns the data in the bounce buffer won't be acceptable.
Change it to align the data the same way it would be aligned in the pagecache.
--- fs/ceph/file.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
fs/ceph/file.c +10 -10
@@ -1572,6 +1572,7 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,bool check_caps = false;struct timespec64 mtime =current_time (inode);size_t count =iov_iter_count (from); +size_t off;if (ceph_snap (file_inode (file)) !=CEPH_NOSNAP )return -EROFS ;
@@ -1609,12 +1610,7 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,break ; } -/* -* write from beginning of first page, -* regardless of io alignment -*/ - num_pages = (len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; - + num_pages =calc_pages_for (pos, len); pages =ceph_alloc_page_vector (num_pages,GFP_KERNEL );if (IS_ERR (pages)) { ret =PTR_ERR (pages);
@@ -1622,9 +1618,12 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos, } left = len; + off =offset_in_page (pos);for (n =0 ; n < num_pages; n++) { -size_t plen =min_t (size_t, left,PAGE_SIZE ); - ret =copy_page_from_iter (pages[n],0 , plen, from); +size_t plen =min_t (size_t, left,PAGE_SIZE - off); + + ret =copy_page_from_iter (pages[n], off, plen, from); + off =0 ;if (ret != plen) { ret = -EFAULT ;break ;
@@ -1639,8 +1638,9 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos, req->r_inode = inode; -osd_req_op_extent_osd_data_pages (req,0 , pages, len,0 , - false, true); +osd_req_op_extent_osd_data_pages (req,0 , pages, len, +offset_in_page (pos), + false, true); req->r_mtime = mtime;ceph_osdc_start_request (&fsc->client->osdc, req);
Signature · 1 line
2.37.2
PATCH v15 18/29 ceph: add read/modify/write to ceph_sync_write
In reply to Jeff LaytonWhen doing a synchronous write on an encrypted inode, we have no guarantee that the caller is writing crypto block-aligned data. When that happens, we must do a read/modify/write cycle.
First, expand the range to cover complete blocks. If we had to change the original pos or length, issue a read to fill the first and/or last pages, and fetch the version of the object from the result.
We then copy data into the pages as usual, encrypt the result and issue a write prefixed by an assertion that the version hasn't changed. If it has changed then we restart the whole thing again.
If there is no object at that position in the file (-ENOENT), we prefix the write on an exclusive create of the object instead.
--- fs/ceph/file.c | 316 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 288 insertions(+), 28 deletions(-)
fs/ceph/file.c +288 -28
@@ -1561,18 +1561,16 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,struct inode *inode =file_inode (file);struct ceph_inode_info *ci =ceph_inode (inode);struct ceph_fs_client *fsc =ceph_inode_to_client (inode); -struct ceph_vino vino; +struct ceph_osd_client *osdc = &fsc->client->osdc;struct ceph_osd_request *req;struct page **pages;u64 len;int num_pages;int written =0 ; -int flags;int ret;bool check_caps = false;struct timespec64 mtime =current_time (inode);size_t count =iov_iter_count (from); -size_t off;if (ceph_snap (file_inode (file)) !=CEPH_NOSNAP )return -EROFS ;
@@ -1592,29 +1590,234 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,if (ret <0 )dout ("invalidate_inode_pages2_range returned %d\n" , ret); - flags =/* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE ; -while ((len =iov_iter_count (from)) >0 ) {size_t left;int n; +u64 write_pos = pos; +u64 write_len = len; +u64 objnum, objoff; +u32 xlen; +u64 assert_ver =0 ; +bool rmw; +bool first, last; +struct iov_iter saved_iter = *from; +size_t off; + +ceph_fscrypt_adjust_off_and_len (inode, &write_pos, &write_len); + +/* clamp the length to the end of first object */ +ceph_calc_file_object_mapping (&ci->i_layout, write_pos, + write_len, &objnum, &objoff, + &xlen); + write_len = xlen; + +/* adjust len downward if it goes beyond current object */ +if (pos + len > write_pos + write_len) + len = write_pos + write_len - pos; - vino =ceph_vino (inode); - req =ceph_osdc_new_request (&fsc->client->osdc, &ci->i_layout, - vino, pos, &len,0 ,1 , -CEPH_OSD_OP_WRITE , flags, snapc, - ci->i_truncate_seq, - ci->i_truncate_size, - false); -if (IS_ERR (req)) { - ret =PTR_ERR (req); -break ; - } +/* +* If we had to adjust the length or position to align with a +* crypto block, then we must do a read/modify/write cycle. We +* use a version assertion to redrive the thing if something +* changes in between. +*/ + first = pos != write_pos; + last = (pos + len) != (write_pos + write_len); + rmw = first || last; - num_pages =calc_pages_for (pos, len); +dout ("sync_write ino %llx %lld~%llu adjusted %lld~%llu -- %srmw\n" , + ci->i_vino.ino, pos, len, write_pos, write_len, rmw ?"" :"no " ); + +/* +* The data is emplaced into the page as it would be if it were in +* an array of pagecache pages. +*/ + num_pages =calc_pages_for (write_pos, write_len); pages =ceph_alloc_page_vector (num_pages,GFP_KERNEL );if (IS_ERR (pages)) { ret =PTR_ERR (pages); - goto out; +break ; + } + +/* Do we need to preload the pages? */ +if (rmw) { +u64 first_pos = write_pos; +u64 last_pos = (write_pos + write_len) -CEPH_FSCRYPT_BLOCK_SIZE ; +u64 read_len =CEPH_FSCRYPT_BLOCK_SIZE ; +struct ceph_osd_req_op *op; + +/* We should only need to do this for encrypted inodes */ +WARN_ON_ONCE (!IS_ENCRYPTED (inode)); + +/* No need to do two reads if first and last blocks are same */ +if (first && last_pos == first_pos) + last = false; + +/* +* Allocate a read request for one or two extents, depending +* on how the request was aligned. +*/ + req =ceph_osdc_new_request (osdc, &ci->i_layout, + ci->i_vino, first ? first_pos : last_pos, + &read_len,0 , (first && last) ?2 :1 , +CEPH_OSD_OP_SPARSE_READ ,CEPH_OSD_FLAG_READ , +NULL , ci->i_truncate_seq, + ci->i_truncate_size, false); +if (IS_ERR (req)) { +ceph_release_page_vector (pages, num_pages); + ret =PTR_ERR (req); +break ; + } + +/* Something is misaligned! */ +if (read_len !=CEPH_FSCRYPT_BLOCK_SIZE ) { +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); + ret = -EIO ; +break ; + } + +/* Add extent for first block? */ + op = &req->r_ops[0 ]; + +if (first) { +osd_req_op_extent_osd_data_pages (req,0 , pages, +CEPH_FSCRYPT_BLOCK_SIZE , +offset_in_page (first_pos), + false, false); +/* We only expect a single extent here */ + ret =__ceph_alloc_sparse_ext_map (op,1 ); +if (ret) { +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); +break ; + } + } + +/* Add extent for last block */ +if (last) { +/* Init the other extent if first extent has been used */ +if (first) { + op = &req->r_ops[1 ]; +osd_req_op_extent_init (req,1 ,CEPH_OSD_OP_SPARSE_READ , + last_pos,CEPH_FSCRYPT_BLOCK_SIZE , + ci->i_truncate_size, + ci->i_truncate_seq); + } + + ret =__ceph_alloc_sparse_ext_map (op,1 ); +if (ret) { +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); +break ; + } + +osd_req_op_extent_osd_data_pages (req, first ?1 :0 , + &pages[num_pages -1 ], +CEPH_FSCRYPT_BLOCK_SIZE , +offset_in_page (last_pos), + false, false); + } + +ceph_osdc_start_request (osdc, req); + ret =ceph_osdc_wait_request (osdc, req); + +/* FIXME: length field is wrong if there are 2 extents */ +ceph_update_read_metrics (&fsc->mdsc->metric, + req->r_start_latency, + req->r_end_latency, + read_len, ret); + +/* Ok if object is not already present */ +if (ret == -ENOENT ) { +/* +* If there is no object, then we can't assert +* on its version. Set it to 0, and we'll use an +* exclusive create instead. +*/ +ceph_osdc_put_request (req); + ret =0 ; + +/* +* zero out the soon-to-be uncopied parts of the +* first and last pages. +*/ +if (first) +zero_user_segment (pages[0 ],0 , +offset_in_page (first_pos)); +if (last) +zero_user_segment (pages[num_pages -1 ], +offset_in_page (last_pos), +PAGE_SIZE ); + }else { +if (ret <0 ) { +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); +break ; + } + + op = &req->r_ops[0 ]; +if (op->extent.sparse_ext_cnt ==0 ) { +if (first) +zero_user_segment (pages[0 ],0 , +offset_in_page (first_pos)); +else +zero_user_segment (pages[num_pages -1 ], +offset_in_page (last_pos), +PAGE_SIZE ); + }else if (op->extent.sparse_ext_cnt !=1 || +ceph_sparse_ext_map_end (op) != +CEPH_FSCRYPT_BLOCK_SIZE ) { + ret = -EIO ; +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); +break ; + } + +if (first && last) { + op = &req->r_ops[1 ]; +if (op->extent.sparse_ext_cnt ==0 ) { +zero_user_segment (pages[num_pages -1 ], +offset_in_page (last_pos), +PAGE_SIZE ); + }else if (op->extent.sparse_ext_cnt !=1 || +ceph_sparse_ext_map_end (op) != +CEPH_FSCRYPT_BLOCK_SIZE ) { + ret = -EIO ; +ceph_osdc_put_request (req); +ceph_release_page_vector (pages, num_pages); +break ; + } + } + +/* Grab assert version. It must be non-zero. */ + assert_ver = req->r_version; +WARN_ON_ONCE (ret >0 && assert_ver ==0 ); + +ceph_osdc_put_request (req); +if (first) { + ret =ceph_fscrypt_decrypt_block_inplace (inode, + pages[0 ], +CEPH_FSCRYPT_BLOCK_SIZE , +offset_in_page (first_pos), + first_pos >>CEPH_FSCRYPT_BLOCK_SHIFT ); +if (ret <0 ) { +ceph_release_page_vector (pages, num_pages); +break ; + } + } +if (last) { + ret =ceph_fscrypt_decrypt_block_inplace (inode, + pages[num_pages -1 ], +CEPH_FSCRYPT_BLOCK_SIZE , +offset_in_page (last_pos), + last_pos >>CEPH_FSCRYPT_BLOCK_SHIFT ); +if (ret <0 ) { +ceph_release_page_vector (pages, num_pages); +break ; + } + } + } } left = len;
@@ -1622,35 +1825,90 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,for (n =0 ; n < num_pages; n++) {size_t plen =min_t (size_t, left,PAGE_SIZE - off); +/* copy the data */ ret =copy_page_from_iter (pages[n], off, plen, from); - off =0 ;if (ret != plen) { ret = -EFAULT ;break ; } + off =0 ; left -= ret; } -if (ret <0 ) { +dout ("sync_write write failed with %d\n" , ret);ceph_release_page_vector (pages, num_pages); - goto out; +break ; } - req->r_inode = inode; +if (IS_ENCRYPTED (inode)) { + ret =ceph_fscrypt_encrypt_pages (inode, pages, + write_pos, write_len, +GFP_KERNEL ); +if (ret <0 ) { +dout ("encryption failed with %d\n" , ret); +ceph_release_page_vector (pages, num_pages); +break ; + } + } -osd_req_op_extent_osd_data_pages (req,0 , pages, len, -offset_in_page (pos), - false, true); + req =ceph_osdc_new_request (osdc, &ci->i_layout, + ci->i_vino, write_pos, &write_len, + rmw ?1 :0 , rmw ?2 :1 , +CEPH_OSD_OP_WRITE , +CEPH_OSD_FLAG_WRITE , + snapc, ci->i_truncate_seq, + ci->i_truncate_size, false); +if (IS_ERR (req)) { + ret =PTR_ERR (req); +ceph_release_page_vector (pages, num_pages); +break ; + } +dout ("sync_write write op %lld~%llu\n" , write_pos, write_len); +osd_req_op_extent_osd_data_pages (req, rmw ?1 :0 , pages, write_len, +offset_in_page (write_pos), false, + true); + req->r_inode = inode; req->r_mtime = mtime; -ceph_osdc_start_request (&fsc->client->osdc, req); - ret =ceph_osdc_wait_request (&fsc->client->osdc, req); + +/* Set up the assertion */ +if (rmw) { +/* +* Set up the assertion. If we don't have a version number, +* then the object doesn't exist yet. Use an exclusive create +* instead of a version assertion in that case. +*/ +if (assert_ver) { +osd_req_op_init (req,0 ,CEPH_OSD_OP_ASSERT_VER ,0 ); + req->r_ops[0 ].assert_ver.ver = assert_ver; + }else { +osd_req_op_init (req,0 ,CEPH_OSD_OP_CREATE , +CEPH_OSD_OP_FLAG_EXCL ); + } + } + +ceph_osdc_start_request (osdc, req); + ret =ceph_osdc_wait_request (osdc, req);ceph_update_write_metrics (&fsc->mdsc->metric, req->r_start_latency, req->r_end_latency, len, ret); -out:ceph_osdc_put_request (req);if (ret !=0 ) { +dout ("sync_write osd write returned %d\n" , ret); +/* Version changed! Must re-do the rmw cycle */ +if ((assert_ver && (ret == -ERANGE || ret == -EOVERFLOW )) || + (!assert_ver && ret == -EEXIST )) { +/* We should only ever see this on a rmw */ +WARN_ON_ONCE (!rmw); + +/* The version should never go backward */ +WARN_ON_ONCE (ret == -EOVERFLOW ); + + *from = saved_iter; + +/* FIXME: limit number of times we loop? */ +continue ; + }ceph_set_error_write (ci);break ; }
@@ -1658,6 +1916,7 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,ceph_clear_error_write (ci); pos += len; written += len; +dout ("sync_write written %d\n" , written);if (pos >i_size_read (inode)) { check_caps =ceph_inode_set_size (inode, pos);if (check_caps)
@@ -1672,6 +1931,7 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos, ret = written; iocb->ki_pos = pos; } +dout ("sync_write returning %d\n" , ret);return ret; }
Signature · 1 line
2.37.2
PATCH v15 19/29 ceph: plumb in decryption during sync reads
In reply to Jeff LaytonSwitch to using sparse reads when the inode is encrypted.
Note that the crypto block may be smaller than a page, but the reverse cannot be true.
--- fs/ceph/file.c | 89 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 24 deletions(-)
fs/ceph/file.c +65 -24
@@ -962,7 +962,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,u64 off = *ki_pos;u64 len =iov_iter_count (to);u64 i_size =i_size_read (inode); -bool sparse =ceph_test_mount_opt (fsc,SPARSEREAD ); +bool sparse =IS_ENCRYPTED (inode) ||ceph_test_mount_opt (fsc,SPARSEREAD );u64 objver =0 ;dout ("sync_read on inode %p %llx~%llx\n" , inode, *ki_pos, len);
@@ -990,10 +990,19 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,int idx;size_t left;struct ceph_osd_req_op *op; +u64 read_off = off; +u64 read_len = len; + +/* determine new offset/length if encrypted */ +ceph_fscrypt_adjust_off_and_len (inode, &read_off, &read_len); + +dout ("sync_read orig %llu~%llu reading %llu~%llu" , + off, len, read_off, read_len); req =ceph_osdc_new_request (osdc, &ci->i_layout, - ci->i_vino, off, &len,0 ,1 , - sparse ?CEPH_OSD_OP_SPARSE_READ :CEPH_OSD_OP_READ , + ci->i_vino, read_off, &read_len,0 ,1 , + sparse ?CEPH_OSD_OP_SPARSE_READ : +CEPH_OSD_OP_READ ,CEPH_OSD_FLAG_READ ,NULL , ci->i_truncate_seq, ci->i_truncate_size, false);
@@ -1002,10 +1011,13 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,break ; } +/* adjust len downward if the request truncated the len */ +if (off + len > read_off + read_len) + len = read_off + read_len - off; more = len <iov_iter_count (to); - num_pages =calc_pages_for (off, len); - page_off = off & ~PAGE_MASK ; + num_pages =calc_pages_for (read_off, read_len); + page_off =offset_in_page (off); pages =ceph_alloc_page_vector (num_pages,GFP_KERNEL );if (IS_ERR (pages)) {ceph_osdc_put_request (req);
@@ -1013,7 +1025,8 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,break ; } -osd_req_op_extent_osd_data_pages (req,0 , pages, len, page_off, +osd_req_op_extent_osd_data_pages (req,0 , pages, read_len, +offset_in_page (read_off), false, false); op = &req->r_ops[0 ];
@@ -1031,7 +1044,7 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,ceph_update_read_metrics (&fsc->mdsc->metric, req->r_start_latency, req->r_end_latency, - len, ret); + read_len, ret);if (ret >0 ) objver = req->r_version;
@@ -1046,8 +1059,34 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,else if (ret == -ENOENT ) ret =0 ; +if (ret >0 &&IS_ENCRYPTED (inode)) { +int fret; + + fret =ceph_fscrypt_decrypt_extents (inode, pages, read_off, + op->extent.sparse_ext, op->extent.sparse_ext_cnt); +if (fret <0 ) { + ret = fret; +ceph_osdc_put_request (req); +break ; + } + +/* account for any partial block at the beginning */ + fret -= (off - read_off); + +/* +* Short read after big offset adjustment? +* Nothing is usable, just call it a zero +* len read. +*/ + fret =max (fret,0 ); + +/* account for partial block at the end */ + ret =min_t (ssize_t, fret, len); + } +ceph_osdc_put_request (req); +/* Short read but not EOF? Zero out the remainder. */ if (ret >=0 && ret < len && (off + ret < i_size)) {int zlen =min (len - ret, i_size - off - ret);int zoff = page_off + ret;
@@ -1061,15 +1100,16 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos, idx =0 ; left = ret >0 ? ret :0 ;while (left >0 ) { -size_t len, copied; - page_off = off & ~PAGE_MASK ; - len =min_t (size_t, left,PAGE_SIZE - page_off); +size_t plen, copied; + + plen =min_t (size_t, left,PAGE_SIZE - page_off);SetPageUptodate (pages[idx]); copied =copy_page_to_iter (pages[idx++], - page_off, len, to); + page_off, plen, to); off += copied; left -= copied; -if (copied < len) { + page_off =0 ; +if (copied < plen) { ret = -EFAULT ;break ; }
@@ -1086,20 +1126,21 @@ ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,break ; } -if (off > *ki_pos) { -if (off >= i_size) { - *retry_op =CHECK_EOF ; - ret = i_size - *ki_pos; - *ki_pos = i_size; - }else { - ret = off - *ki_pos; - *ki_pos = off; +if (ret >0 ) { +if (off > *ki_pos) { +if (off >= i_size) { + *retry_op =CHECK_EOF ; + ret = i_size - *ki_pos; + *ki_pos = i_size; + }else { + ret = off - *ki_pos; + *ki_pos = off; + } } - } - -if (last_objver && ret >0 ) - *last_objver = objver; +if (last_objver) + *last_objver = objver; + }dout ("sync_read result %zd retry_op %d\n" , ret, *retry_op);return ret; }
Signature · 1 line
2.37.2
PATCH v15 20/29 ceph: add fscrypt decryption support to ceph_netfs_issue_op
In reply to Jeff LaytonForce the use of sparse reads when the inode is encrypted, and add the appropriate code to decrypt the extent map after receiving.
--- fs/ceph/addr.c | 64 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-)
fs/ceph/addr.c +55 -9
@@ -18,6 +18,7 @@#include "mds_client.h" #include "cache.h" #include "metric.h" +#include "crypto.h" #include <linux/ceph/osd_client.h> #include <linux/ceph/striper.h>
@@ -216,7 +217,8 @@ static bool ceph_netfs_clamp_length(struct netfs_io_subrequest *subreq)static void finish_netfs_read (struct ceph_osd_request *req) { -struct ceph_fs_client *fsc =ceph_inode_to_client (req->r_inode); +struct inode *inode = req->r_inode; +struct ceph_fs_client *fsc =ceph_inode_to_client (inode);struct ceph_osd_data *osd_data =osd_req_op_extent_osd_data (req,0 );struct netfs_io_subrequest *subreq = req->r_priv;struct ceph_osd_req_op *op = &req->r_ops[0 ];
@@ -230,16 +232,30 @@ static void finish_netfs_read(struct ceph_osd_request *req) subreq->len, i_size_read(req->r_inode));/* no object means success but no data */ -if (sparse && err >=0 ) - err =ceph_sparse_ext_map_end (op); -else if (err == -ENOENT ) +if (err == -ENOENT ) err =0 ;else if (err == -EBLOCKLISTED ) fsc->blocklisted = true; -if (err >=0 && err < subreq->len) -__set_bit (NETFS_SREQ_CLEAR_TAIL , &subreq->flags); +if (err >=0 ) { +if (sparse && err >0 ) + err =ceph_sparse_ext_map_end (op); +if (err < subreq->len) +__set_bit (NETFS_SREQ_CLEAR_TAIL , &subreq->flags); +if (IS_ENCRYPTED (inode) && err >0 ) { + err =ceph_fscrypt_decrypt_extents (inode, osd_data->pages, + subreq->start, op->extent.sparse_ext, + op->extent.sparse_ext_cnt); +if (err > subreq->len) + err = subreq->len; + } + } +if (osd_data->type ==CEPH_OSD_DATA_TYPE_PAGES ) { +ceph_put_page_vector (osd_data->pages, +calc_pages_for (osd_data->alignment, + osd_data->length), false); + }netfs_subreq_terminated (subreq, err, false);iput (req->r_inode); }
@@ -310,12 +326,15 @@ static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)struct iov_iter iter;int err =0 ;u64 len = subreq->len; -bool sparse =ceph_test_mount_opt (fsc,SPARSEREAD ); +bool sparse =IS_ENCRYPTED (inode) ||ceph_test_mount_opt (fsc,SPARSEREAD ); +u64 off = subreq->start;if (ceph_has_inline_data (ci) &&ceph_netfs_issue_op_inline (subreq))return ; - req =ceph_osdc_new_request (&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len, +ceph_fscrypt_adjust_off_and_len (inode, &off, &len); + + req =ceph_osdc_new_request (&fsc->client->osdc, &ci->i_layout, vino, off, &len,0 ,1 , sparse ?CEPH_OSD_OP_SPARSE_READ :CEPH_OSD_OP_READ ,CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,NULL , ci->i_truncate_seq, ci->i_truncate_size, false);
@@ -334,8 +353,35 @@ static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq) }dout ("%s: pos=%llu orig_len=%zu len=%llu\n" , __func__, subreq->start, subreq->len, len); +iov_iter_xarray (&iter,READ , &rreq->mapping->i_pages, subreq->start, len); -osd_req_op_extent_osd_iter (req,0 , &iter); + +/* +* FIXME: For now, use CEPH_OSD_DATA_TYPE_PAGES instead of _ITER for +* encrypted inodes. We'd need infrastructure that handles an iov_iter +* instead of page arrays, and we don't have that as of yet. Once the +* dust settles on the write helpers and encrypt/decrypt routines for +* netfs, we should be able to rework this. +*/ +if (IS_ENCRYPTED (inode)) { +struct page **pages; +size_t page_off; + + err =iov_iter_get_pages_alloc2 (&iter, &pages, len, &page_off); +if (err <0 ) { +dout ("%s: iov_ter_get_pages_alloc returned %d\n" , __func__, err); + goto out; + } + +/* should always give us a page-aligned read */ +WARN_ON_ONCE (page_off); + len = err; + err =0 ; + +osd_req_op_extent_osd_data_pages (req,0 , pages, len,0 , false, false); + }else { +osd_req_op_extent_osd_iter (req,0 , &iter); + } req->r_callback = finish_netfs_read; req->r_priv = subreq; req->r_inode = inode;
Signature · 1 line
2.37.2
PATCH v15 21/29 ceph: set i_blkbits to crypto block size for encrypted inodes
In reply to Jeff LaytonSome of the underlying infrastructure for fscrypt relies on i_blkbits being aligned to the crypto blocksize.
--- fs/ceph/inode.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
fs/ceph/inode.c +9 -7
@@ -988,13 +988,6 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page, issued |=__ceph_caps_dirty (ci); new_issued = ~issued & info_caps; -/* directories have fl_stripe_unit set to zero */ -if (le32_to_cpu (info->layout.fl_stripe_unit)) - inode->i_blkbits = -fls (le32_to_cpu (info->layout.fl_stripe_unit)) -1 ; -else - inode->i_blkbits =CEPH_BLOCK_SHIFT ; -__ceph_update_quota (ci, iinfo->max_bytes, iinfo->max_files);#ifdef CONFIG_FS_ENCRYPTION
@@ -1020,6 +1013,15 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page,ceph_decode_timespec64 (&ci->i_snap_btime, &iinfo->snap_btime); } +/* directories have fl_stripe_unit set to zero */ +if (IS_ENCRYPTED (inode)) + inode->i_blkbits =CEPH_FSCRYPT_BLOCK_SHIFT ; +else if (le32_to_cpu (info->layout.fl_stripe_unit)) + inode->i_blkbits = +fls (le32_to_cpu (info->layout.fl_stripe_unit)) -1 ; +else + inode->i_blkbits =CEPH_BLOCK_SHIFT ; +if ((new_version || (new_issued &CEPH_CAP_LINK_SHARED )) && (issued &CEPH_CAP_LINK_EXCL ) ==0 )set_nlink (inode,le32_to_cpu (info->nlink));
Signature · 1 line
2.37.2
PATCH v15 22/29 ceph: add encryption support to writepage
In reply to Jeff LaytonAllow writepage to issue encrypted writes. Extend out the requested size and offset to cover complete blocks, and then encrypt and write them to the OSDs.
--- fs/ceph/addr.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
fs/ceph/addr.c +29 -8
@@ -598,10 +598,12 @@ static int writepage_nounlock(struct page *page, struct writeback_control *wbc)loff_t page_off =page_offset (page);int err;loff_t len =thp_size (page); +loff_t wlen;struct ceph_writeback_ctl ceph_wbc;struct ceph_osd_client *osdc = &fsc->client->osdc;struct ceph_osd_request *req;bool caching =ceph_is_cache_enabled (inode); +struct page *bounce_page =NULL ;dout ("writepage %p idx %lu\n" , page, page->index);
@@ -634,31 +636,50 @@ static int writepage_nounlock(struct page *page, struct writeback_control *wbc)if (ceph_wbc.i_size < page_off + len) len = ceph_wbc.i_size - page_off; + wlen =IS_ENCRYPTED (inode) ?round_up (len,CEPH_FSCRYPT_BLOCK_SIZE ) : len;dout ("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n" , - inode, page, page->index, page_off, len, snapc, snapc->seq); + inode, page, page->index, page_off, wlen, snapc, snapc->seq);if (atomic_long_inc_return (&fsc->writeback_count) >CONGESTION_ON_THRESH (fsc->mount_options->congestion_kb)) fsc->write_congested = true; - req =ceph_osdc_new_request (osdc, &ci->i_layout,ceph_vino (inode), page_off, &len,0 ,1 , -CEPH_OSD_OP_WRITE ,CEPH_OSD_FLAG_WRITE , snapc, - ceph_wbc.truncate_seq, ceph_wbc.truncate_size, - true); + req =ceph_osdc_new_request (osdc, &ci->i_layout,ceph_vino (inode), + page_off, &wlen,0 ,1 ,CEPH_OSD_OP_WRITE , +CEPH_OSD_FLAG_WRITE , snapc, + ceph_wbc.truncate_seq, + ceph_wbc.truncate_size, true);if (IS_ERR (req)) {redirty_page_for_writepage (wbc, page);return PTR_ERR (req); } +if (wlen < len) + len = wlen; +set_page_writeback (page);if (caching)ceph_set_page_fscache (page);ceph_fscache_write_to_cache (inode, page_off, len, caching); +if (IS_ENCRYPTED (inode)) { + bounce_page =fscrypt_encrypt_pagecache_blocks (page,CEPH_FSCRYPT_BLOCK_SIZE , +0 ,GFP_NOFS ); +if (IS_ERR (bounce_page)) { +redirty_page_for_writepage (wbc, page); +end_page_writeback (page); +ceph_osdc_put_request (req); +return PTR_ERR (bounce_page); + } + } +/* it may be a short write due to an object boundary */ WARN_ON_ONCE (len >thp_size (page)); -osd_req_op_extent_osd_data_pages (req,0 , &page, len,0 , false, false); -dout ("writepage %llu~%llu (%llu bytes)\n" , page_off, len, len); +osd_req_op_extent_osd_data_pages (req,0 , + bounce_page ? &bounce_page : &page, wlen,0 , + false, false); +dout ("writepage %llu~%llu (%llu bytes, %sencrypted)\n" , + page_off, len, wlen,IS_ENCRYPTED (inode) ?"" :"not " ); req->r_mtime = inode->i_mtime;ceph_osdc_start_request (osdc, req);
@@ -666,7 +687,7 @@ static int writepage_nounlock(struct page *page, struct writeback_control *wbc)ceph_update_write_metrics (&fsc->mdsc->metric, req->r_start_latency, req->r_end_latency, len, err); - +fscrypt_free_bounce_page (bounce_page);ceph_osdc_put_request (req);if (err ==0 ) err = len;
Signature · 1 line
2.37.2
PATCH v15 23/29 ceph: fscrypt support for writepages
In reply to Jeff LaytonAdd the appropriate machinery to write back dirty data with encryption.
--- fs/ceph/addr.c | 63 +++++++++++++++++++++++++++++++++++++++--------- fs/ceph/crypto.h | 18 +++++++++++++- 2 files changed, 68 insertions(+), 13 deletions(-)
fs/ceph/addr.c +51 -12
@@ -559,10 +559,12 @@ static u64 get_writepages_data_length(struct inode *inode, struct page *page, u64 start) {struct ceph_inode_info *ci =ceph_inode (inode); -struct ceph_snap_context *snapc =page_snap_context (page); +struct ceph_snap_context *snapc;struct ceph_cap_snap *capsnap =NULL ;u64 end =i_size_read (inode); +u64 ret; + snapc =page_snap_context (ceph_fscrypt_pagecache_page (page));if (snapc != ci->i_head_snapc) {bool found = false;spin_lock (&ci->i_ceph_lock);
@@ -577,9 +579,12 @@ static u64 get_writepages_data_length(struct inode *inode,spin_unlock (&ci->i_ceph_lock);WARN_ON (!found); } -if (end >page_offset (page) +thp_size (page)) - end =page_offset (page) +thp_size (page); -return end > start ? end - start :0 ; +if (end >ceph_fscrypt_page_offset (page) +thp_size (page)) + end =ceph_fscrypt_page_offset (page) +thp_size (page); + ret = end > start ? end - start :0 ; +if (ret &&fscrypt_is_bounce_page (page)) + ret =round_up (ret,CEPH_FSCRYPT_BLOCK_SIZE ); +return ret; }/*
@@ -806,6 +811,11 @@ static void writepages_finish(struct ceph_osd_request *req) total_pages += num_pages;for (j =0 ; j < num_pages; j++) { page = osd_data->pages[j]; +if (fscrypt_is_bounce_page (page)) { + page =fscrypt_pagecache_page (page); +fscrypt_free_bounce_page (osd_data->pages[j]); + osd_data->pages[j] = page; + }BUG_ON (!page);WARN_ON (!PageUptodate (page));
@@ -1067,9 +1077,28 @@ static int ceph_writepages_start(struct address_space *mapping, fsc->mount_options->congestion_kb)) fsc->write_congested = true; - pages[locked_pages++] = page; - pvec.pages[i] =NULL ; +if (IS_ENCRYPTED (inode)) { + pages[locked_pages] = +fscrypt_encrypt_pagecache_blocks (page, +PAGE_SIZE ,0 , + locked_pages ?GFP_NOWAIT :GFP_NOFS ); +if (IS_ERR (pages[locked_pages])) { +if (PTR_ERR (pages[locked_pages]) == -EINVAL ) +pr_err ("%s: inode->i_blkbits=%hhu\n" , + __func__, inode->i_blkbits); +/* better not fail on first page! */ +BUG_ON (locked_pages ==0 ); + pages[locked_pages] =NULL ; +redirty_page_for_writepage (wbc, page); +unlock_page (page); +break ; + } + ++locked_pages; + }else { + pages[locked_pages++] = page; + } + pvec.pages[i] =NULL ; len +=thp_size (page); }
@@ -1097,7 +1126,7 @@ static int ceph_writepages_start(struct address_space *mapping, } new_request: - offset =page_offset (pages[0 ]); + offset =ceph_fscrypt_page_offset (pages[0 ]); len = wsize; req = ceph_osdc_new_request(&fsc->client->osdc,
@@ -1118,8 +1147,8 @@ static int ceph_writepages_start(struct address_space *mapping, ceph_wbc.truncate_size, true);BUG_ON (IS_ERR (req)); } -BUG_ON (len <page_offset (pages[locked_pages -1 ]) + -thp_size (page) - offset); +BUG_ON (len <ceph_fscrypt_page_offset (pages[locked_pages -1 ]) + +thp_size (pages[locked_pages -1 ]) - offset); req->r_callback = writepages_finish; req->r_inode = inode;
@@ -1129,7 +1158,9 @@ static int ceph_writepages_start(struct address_space *mapping, data_pages = pages; op_idx =0 ;for (i =0 ; i < locked_pages; i++) { -u64 cur_offset =page_offset (pages[i]); +struct page *page =ceph_fscrypt_pagecache_page (pages[i]); + +u64 cur_offset =page_offset (page);/* * Discontinuity in page range? Ceph can handle that by just passing * multiple extents in the write op.
@@ -1158,9 +1189,9 @@ static int ceph_writepages_start(struct address_space *mapping, op_idx++; } -set_page_writeback (pages[i]); +set_page_writeback (page);if (caching) -ceph_set_page_fscache (pages[i]); +ceph_set_page_fscache (page); len +=thp_size (page); }ceph_fscache_write_to_cache (inode, offset, len, caching);
@@ -1176,8 +1207,16 @@ static int ceph_writepages_start(struct address_space *mapping, offset); len =max (len, min_len); } +if (IS_ENCRYPTED (inode)) + len =round_up (len,CEPH_FSCRYPT_BLOCK_SIZE ); +dout ("writepages got pages at %llu~%llu\n" , offset, len); +if (IS_ENCRYPTED (inode) && + ((offset | len) & ~CEPH_FSCRYPT_BLOCK_MASK )) +pr_warn ("%s: bad encrypted write offset=%lld len=%llu\n" , + __func__, offset, len); +osd_req_op_extent_osd_data_pages (req, op_idx, data_pages, len,0 , from_pool, false);osd_req_op_extent_update (req, op_idx, len);
fs/ceph/crypto.h +17 -1
@@ -154,6 +154,12 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of struct ceph_sparse_extent *map, u32 ext_cnt);int ceph_fscrypt_encrypt_pages (struct inode *inode,struct page **page,u64 off,int len,gfp_t gfp); + +static inline struct page *ceph_fscrypt_pagecache_page (struct page *page) +{ +return fscrypt_is_bounce_page (page) ?fscrypt_pagecache_page (page) : page; +} +#else /* CONFIG_FS_ENCRYPTION */ static inline void ceph_fscrypt_set_ops (struct super_block *sb)
@@ -243,6 +249,16 @@ static inline int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page ** {return 0 ; } + +static inline struct page *ceph_fscrypt_pagecache_page (struct page *page) +{ +return page; +}#endif /* CONFIG_FS_ENCRYPTION */ -#endif +static inline loff_t ceph_fscrypt_page_offset (struct page *page) +{ +return page_offset (ceph_fscrypt_pagecache_page (page)); +} + +#endif /* _CEPH_CRYPTO_H */
Signature · 1 line
2.37.2
PATCH v15 24/29 ceph: invalidate pages when doing direct/sync writes
In reply to Jeff LaytonFrom: Luís Henriques <[email protected]>
When doing a direct/sync write, we need to invalidate the page cache in the range being written to. If we don't do this, the cache will include invalid data as we just did a write that avoided the page cache.
In the event that invalidation fails, just ignore the error. That likely just means that we raced with another task doing a buffered write, in which case we want to leave the page intact anyway.
[ jlayton: minor comment update ]
--- fs/ceph/file.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
fs/ceph/file.c +14 -5
@@ -1625,11 +1625,6 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,return ret;ceph_fscache_invalidate (inode, false); - ret =invalidate_inode_pages2_range (inode->i_mapping, - pos >>PAGE_SHIFT , - (pos + count -1 ) >>PAGE_SHIFT ); -if (ret <0 ) -dout ("invalidate_inode_pages2_range returned %d\n" , ret);while ((len =iov_iter_count (from)) >0 ) {size_t left;
@@ -1955,6 +1950,20 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos, }ceph_clear_error_write (ci); + +/* +* We successfully wrote to a range of the file. Declare +* that region of the pagecache invalid. +*/ + ret =invalidate_inode_pages2_range ( + inode->i_mapping, + pos >>PAGE_SHIFT , + (pos + len -1 ) >>PAGE_SHIFT ); +if (ret <0 ) { +dout ("invalidate_inode_pages2_range returned %d\n" , + ret); + ret =0 ; + } pos += len; written += len;dout ("sync_write written %d\n" , written);
Signature · 1 line
2.37.2
PATCH v15 25/29 ceph: add support for encrypted snapshot names
In reply to Jeff LaytonFrom: Luís Henriques <[email protected]>
Since filenames in encrypted directories are already encrypted and shown as a base64-encoded string when the directory is locked, snapshot names should show a similar behaviour.
--- fs/ceph/inode.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
fs/ceph/inode.c +29 -4
@@ -92,9 +92,15 @@ struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,if (err <0 ) goto out_err; - err =ceph_fscrypt_prepare_context (dir, inode, as_ctx); -if (err) - goto out_err; +/* +* We'll skip setting fscrypt context for snapshots, leaving that for +* the handle_reply(). +*/ +if (ceph_snap (dir) !=CEPH_SNAPDIR ) { + err =ceph_fscrypt_prepare_context (dir, inode, as_ctx); +if (err) + goto out_err; + }return inode; out_err:
@@ -158,6 +164,7 @@ struct inode *ceph_get_snapdir(struct inode *parent) };struct inode *inode =ceph_get_inode (parent->i_sb, vino,NULL );struct ceph_inode_info *ci =ceph_inode (inode); +int ret = -ENOTDIR ;if (IS_ERR (inode))return inode;
@@ -183,6 +190,24 @@ struct inode *ceph_get_snapdir(struct inode *parent) ci->i_rbytes =0 ; ci->i_btime =ceph_inode (parent)->i_btime; +#ifdef CONFIG_FS_ENCRYPTION +/* if encrypted, just borrow fscrypt_auth from parent */ +if (IS_ENCRYPTED (parent)) { +struct ceph_inode_info *pci =ceph_inode (parent); + + ci->fscrypt_auth =kmemdup (pci->fscrypt_auth, + pci->fscrypt_auth_len, +GFP_KERNEL ); +if (ci->fscrypt_auth) { + inode->i_flags |=S_ENCRYPTED ; + ci->fscrypt_auth_len = pci->fscrypt_auth_len; + }else { +dout ("Failed to alloc snapdir fscrypt_auth\n" ); + ret = -ENOMEM ; + goto err; + } + } +#endif if (inode->i_state &I_NEW ) { inode->i_op = &ceph_snapdir_iops; inode->i_fop = &ceph_snapdir_fops;
@@ -196,7 +221,7 @@ struct inode *ceph_get_snapdir(struct inode *parent)discard_new_inode (inode); elseiput (inode ); -return ERR_PTR (-ENOTDIR ); +return ERR_PTR (ret); }const struct inode_operations ceph_file_iops = {
Signature · 1 line
2.37.2
PATCH v15 26/29 ceph: add support for handling encrypted snapshot names
In reply to Jeff LaytonFrom: Luís Henriques <[email protected]>
When creating a snapshot, the .snap directories for every subdirectory will show the snapshot name in the "long format":
# mkdir .snap/my-snap # ls my-dir/.snap/ _my-snap_1099511627782
Encrypted snapshots will need to be able to handle these snapshot names by encrypting/decrypting only the snapshot part of the string ('my-snap').
Also, since the MDS prevents snapshot names to be bigger than 240 characters it is necessary to adapt CEPH_NOHASH_NAME_MAX to accommodate this extra limitation.
--- fs/ceph/crypto.c | 190 ++++++++++++++++++++++++++++++++++++++++------- fs/ceph/crypto.h | 4 +- 2 files changed, 165 insertions(+), 29 deletions(-)
fs/ceph/crypto.c +163 -27
@@ -189,16 +189,100 @@ void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req, struct ceph_acl_seswap (req->r_fscrypt_auth, as->fscrypt_auth); } -int ceph_encode_encrypted_dname (const struct inode *parent,struct qstr *d_name,char *buf) +/* +* User-created snapshots can't start with '_'. Snapshots that start with this +* character are special (hint: there aren't real snapshots) and use the +* following format: +* +* _<SNAPSHOT-NAME>_<INODE-NUMBER> +* +* where: +* - <SNAPSHOT-NAME> - the real snapshot name that may need to be decrypted, +* - <INODE-NUMBER> - the inode number (in decimal) for the actual snapshot +* +* This function parses these snapshot names and returns the inode +* <INODE-NUMBER>. 'name_len' will also bet set with the <SNAPSHOT-NAME> +* length. +*/ +static struct inode *parse_longname (const struct inode *parent,const char *name, +int *name_len) +{ +struct inode *dir =NULL ; +struct ceph_vino vino = { .snap =CEPH_NOSNAP }; +char *inode_number; +char *name_end; +int orig_len = *name_len; +int ret = -EIO ; + +/* Skip initial '_' */ + name++; + name_end =strrchr (name,'_' ); +if (!name_end) { +dout ("Failed to parse long snapshot name: %s\n" , name); +return ERR_PTR (-EIO ); + } + *name_len = (name_end - name); +if (*name_len <=0 ) { +pr_err ("Failed to parse long snapshot name\n" ); +return ERR_PTR (-EIO ); + } + +/* Get the inode number */ + inode_number =kmemdup_nul (name_end +1 , + orig_len - *name_len -2 , +GFP_KERNEL ); +if (!inode_number) +return ERR_PTR (-ENOMEM ); + ret =kstrtou64 (inode_number,10 , &vino.ino); +if (ret) { +dout ("Failed to parse inode number: %s\n" , name); + dir =ERR_PTR (ret); + goto out; + } + +/* And finally the inode */ + dir =ceph_find_inode (parent->i_sb, vino); +if (!dir) { +/* This can happen if we're not mounting cephfs on the root */ + dir =ceph_get_inode (parent->i_sb, vino,NULL ); +if (!dir) + dir =ERR_PTR (-ENOENT ); + } +if (IS_ERR (dir)) +dout ("Can't find inode %s (%s)\n" , inode_number, name); + +out: +kfree (inode_number); +return dir; +} + +int ceph_encode_encrypted_dname (struct inode *parent,struct qstr *d_name,char *buf) { +struct inode *dir = parent; +struct qstr iname;u32 len; +int name_len;int elen;int ret; -u8 *cryptbuf; +u8 *cryptbuf =NULL ; + + iname.name = d_name->name; + name_len = d_name->len; + +/* Handle the special case of snapshot names that start with '_' */ +if ((ceph_snap (dir) ==CEPH_SNAPDIR ) && (name_len >0 ) && + (iname.name[0 ] =='_' )) { + dir =parse_longname (parent, iname.name, &name_len); +if (IS_ERR (dir)) +return PTR_ERR (dir); + iname.name++;/* skip initial '_' */ + } + iname.len = name_len; -if (!fscrypt_has_encryption_key (parent)) { +if (!fscrypt_has_encryption_key (dir)) {memcpy (buf, d_name->name, d_name->len); -return d_name->len; + elen = d_name->len; + goto out; }/*
@@ -207,18 +291,22 @@ int ceph_encode_encrypted_dname(const struct inode *parent, struct qstr *d_name,* * See: fscrypt_setup_filename */ -if (!fscrypt_fname_encrypted_size (parent, d_name->len,NAME_MAX , &len)) -return -ENAMETOOLONG ; +if (!fscrypt_fname_encrypted_size (dir, iname.len,NAME_MAX , &len)) { + elen = -ENAMETOOLONG ; + goto out; + }/* Allocate a buffer appropriate to hold the result */ cryptbuf =kmalloc (len >CEPH_NOHASH_NAME_MAX ?NAME_MAX : len,GFP_KERNEL ); -if (!cryptbuf) -return -ENOMEM ; +if (!cryptbuf) { + elen = -ENOMEM ; + goto out; + } - ret =fscrypt_fname_encrypt (parent, d_name, cryptbuf, len); + ret =fscrypt_fname_encrypt (dir, &iname, cryptbuf, len);if (ret) { -kfree (cryptbuf); -return ret; + elen = ret; + goto out; }/* hash the end if the name is long enough */
@@ -234,12 +322,30 @@ int ceph_encode_encrypted_dname(const struct inode *parent, struct qstr *d_name,/* base64 encode the encrypted name */ elen =ceph_base64_encode (cryptbuf, len, buf); -kfree (cryptbuf);dout ("base64-encoded ciphertext name = %.*s\n" , elen, buf); + +/* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */ +WARN_ON (elen >240 ); +if ((elen >0 ) && (dir != parent)) { +char tmp_buf[NAME_MAX ]; + + elen =snprintf (tmp_buf,sizeof (tmp_buf),"_%.*s_%ld" , + elen, buf, dir->i_ino); +memcpy (buf, tmp_buf, elen); + } + +out: +kfree (cryptbuf); +if (dir != parent) { +if ((dir->i_state &I_NEW )) +discard_new_inode (dir); +else +iput (dir); + }return elen; } -int ceph_encode_encrypted_fname (const struct inode *parent,struct dentry *dentry,char *buf) +int ceph_encode_encrypted_fname (struct inode *parent,struct dentry *dentry,char *buf) {WARN_ON_ONCE (!fscrypt_has_encryption_key (parent));
@@ -264,29 +370,42 @@ int ceph_encode_encrypted_fname(const struct inode *parent, struct dentry *dentrint ceph_fname_to_usr (const struct ceph_fname *fname,struct fscrypt_str *tname,struct fscrypt_str *oname,bool *is_nokey) { -int ret; +struct inode *dir = fname->dir;struct fscrypt_str _tname =FSTR_INIT (NULL ,0 );struct fscrypt_str iname; - -if (!IS_ENCRYPTED (fname->dir)) { - oname->name = fname->name; - oname->len = fname->name_len; -return 0 ; - } +char *name = fname->name; +int name_len = fname->name_len; +int ret;/* Sanity check that the resulting name will fit in the buffer */ if (fname->name_len >NAME_MAX || fname->ctext_len >NAME_MAX )return -EIO ; - ret =__fscrypt_prepare_readdir (fname->dir); +/* Handle the special case of snapshot names that start with '_' */ +if ((ceph_snap (dir) ==CEPH_SNAPDIR ) && (name_len >0 ) && + (name[0 ] =='_' )) { + dir =parse_longname (dir, name, &name_len); +if (IS_ERR (dir)) +return PTR_ERR (dir); + name++;/* skip initial '_' */ + } + +if (!IS_ENCRYPTED (dir)) { + oname->name = fname->name; + oname->len = fname->name_len; + ret =0 ; + goto out_inode; + } + + ret =__fscrypt_prepare_readdir (dir);if (ret) -return ret; + goto out_inode;/* * Use the raw dentry name as sent by the MDS instead of * generating a nokey name via fscrypt. */ -if (!fscrypt_has_encryption_key (fname->dir)) { +if (!fscrypt_has_encryption_key (dir)) {if (fname->no_copy) oname->name = fname->name;else
@@ -294,7 +413,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, oname->len = fname->name_len;if (is_nokey) *is_nokey = true; -return 0 ; + ret =0 ; + goto out_inode; }if (fname->ctext_len ==0 ) {
@@ -303,11 +423,11 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,if (!tname) { ret =fscrypt_fname_alloc_buffer (NAME_MAX , &_tname);if (ret) -return ret; + goto out_inode; tname = &_tname; } - declen =ceph_base64_decode (fname->name, fname->name_len, tname->name); + declen =ceph_base64_decode (name, name_len, tname->name);if (declen <=0 ) { ret = -EIO ; goto out;
@@ -319,9 +439,25 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, iname.len = fname->ctext_len; } - ret =fscrypt_fname_disk_to_usr (fname->dir,0 ,0 , &iname, oname); + ret =fscrypt_fname_disk_to_usr (dir,0 ,0 , &iname, oname); +if (!ret && (dir != fname->dir)) { +char tmp_buf[CEPH_BASE64_CHARS (NAME_MAX )]; + + name_len =snprintf (tmp_buf,sizeof (tmp_buf),"_%.*s_%ld" , + oname->len, oname->name, dir->i_ino); +memcpy (oname->name, tmp_buf, name_len); + oname->len = name_len; + } + out:fscrypt_fname_free_buffer (&_tname); +out_inode: +if ((dir != fname->dir) && !IS_ERR (dir)) { +if ((dir->i_state &I_NEW )) +discard_new_inode (dir); +else +iput (dir); + }return ret; }
fs/ceph/crypto.h +2 -2
@@ -101,8 +101,8 @@ void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);int ceph_fscrypt_prepare_context (struct inode *dir,struct inode *inode,struct ceph_acl_sec_ctx *as);void ceph_fscrypt_as_ctx_to_req (struct ceph_mds_request *req,struct ceph_acl_sec_ctx *as); -int ceph_encode_encrypted_dname (const struct inode *parent,struct qstr *d_name,char *buf); -int ceph_encode_encrypted_fname (const struct inode *parent,struct dentry *dentry,char *buf); +int ceph_encode_encrypted_dname (struct inode *parent,struct qstr *d_name,char *buf); +int ceph_encode_encrypted_fname (struct inode *parent,struct dentry *dentry,char *buf);static inline int ceph_fname_alloc_buffer (struct inode *parent,struct fscrypt_str *fname) {
Signature · 1 line
2.37.2
PATCH v15 27/29 ceph: update documentation regarding snapshot naming limitations
In reply to Jeff LaytonFrom: Luís Henriques <[email protected]>
--- Documentation/filesystems/ceph.rst | 10 ++++++++++ 1 file changed, 10 insertions(+)
Documentation/filesystems/ceph.rst +10 -0
@@ -57,6 +57,16 @@ a snapshot on any subdirectory (and its nested contents) in the system. Snapshot creation and deletion are as simple as 'mkdir .snap/foo' and 'rmdir .snap/foo'. +Snapshot names have two limitations: + +* They can not start with an underscore ('_'), as these names are reserved + for internal usage by the MDS. +* They can not exceed 240 characters in size. This is because the MDS makes + use of long snapshot names internally, which follow the format: + `_<SNAPSHOT-NAME>_<INODE-NUMBER>`. Since filenames in general can't have + more than 255 characters, and `<node-id>` takes 13 characters, the long + snapshot names can take as much as 255 - 1 - 1 - 13 = 240. + Ceph also provides some recursive accounting on directories for nested files and bytes. That is, a 'getfattr -d foo' on any directory in the system will reveal the total number of nested regular files and
Signature · 1 line
2.37.2
PATCH v15 28/29 ceph: prevent snapshots to be created in encrypted locked directories
In reply to Jeff LaytonFrom: Luís Henriques <[email protected]>
With snapshot names encryption we can not allow snapshots to be created in locked directories because the names wouldn't be encrypted. This patch forces the directory to be unlocked to allow a snapshot to be created.
--- fs/ceph/dir.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/ceph/dir.c +5 -0
@@ -1084,6 +1084,11 @@ static int ceph_mkdir(struct user_namespace *mnt_userns, struct inode *dir, err = -EDQUOT ; goto out; } +if ((op ==CEPH_MDS_OP_MKSNAP ) &&IS_ENCRYPTED (dir) && + !fscrypt_has_encryption_key (dir)) { + err = -ENOKEY ; + goto out; + } req =ceph_mdsc_create_request (mdsc, op,USE_AUTH_MDS );
Signature · 1 line
2.37.2
PATCH v15 29/29 ceph: report STATX_ATTR_ENCRYPTED on encrypted inodes
In reply to Jeff Layton--- fs/ceph/inode.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ceph/inode.c +4 -0
@@ -3000,6 +3000,10 @@ int ceph_getattr(struct user_namespace *mnt_userns, const struct path *path, stat->nlink =1 +1 + ci->i_subdirs; } +if (IS_ENCRYPTED (inode)) + stat->attributes |=STATX_ATTR_ENCRYPTED ; + stat->attributes_mask |=STATX_ATTR_ENCRYPTED ; + stat->result_mask = request_mask & valid_mask;return err; }
Signature · 1 line
2.37.2
Quoted 61 lines · Jeff Layton
v15: rebase onto current ceph/testing branch
add some missingceph_osdc_wait_requestcallsThis patchset represents the remaining patches to add fscrypt support, rebased on top of today's "testing" branch. They're all ceph changes as the vfs and fscrypt patches are now merged!
Note that the current ceph-client/wip-fscrypt branch is broken. It's still based on v5.19-rc6 and has some missing calls to
ceph_osdc_wait_requestthat cause panics. This set fixes that.These are currently in my ceph-fscrypt branch:
https://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux.git/log/?h=ceph-fscrypt
It would be good to update the wip-fscrypt branch with this pile.
Jeff Layton (19):
libceph: addCEPH_OSD_OP_ASSERT_VERsupport
ceph: size handling for encrypted inodes in cap updates
ceph:fscrypt_filefield handling in MClientRequest messages
ceph: get file size fromfscrypt_filewhen present in inode traces
ceph: handle fscrypt fields in cap messages from MDS
ceph: updateWARN_ONmessage topr_warn
ceph: add infrastructure for file encryption and decryption
libceph: allowceph_osdc_new_requestto accept a multi-op read
ceph: disable fallocate for encrypted inodes
ceph: disable copy offload on encrypted inodes
ceph: don't use special DIO path for encrypted inodes
ceph: align data in pages inceph_sync_write
ceph: add read/modify/write toceph_sync_write
ceph: plumb in decryption during sync reads
ceph: add fscrypt decryption support toceph_netfs_issue_op
ceph: seti_blkbitsto crypto block size for encrypted inodes
ceph: add encryption support to writepage
ceph: fscrypt support for writepages
ceph: reportSTATX_ATTR_ENCRYPTEDon encrypted inodesLuís Henriques (6):
ceph: don't allow changing layout on encrypted files/directories
ceph: invalidate pages when doing direct/sync writes
ceph: add support for encrypted snapshot names
ceph: add support for handling encrypted snapshot names
ceph: update documentation regarding snapshot naming limitations
ceph: prevent snapshots to be created in encrypted locked directoriesXiubo Li (4):
ceph: add__ceph_get_capshelper support
ceph: add__ceph_sync_readhelper support
ceph: add object version support for sync read
ceph: add truncate size handling support for fscryptDocumentation/filesystems/ceph.rst | 10 + fs/ceph/addr.c | 164 ++++++++-- fs/ceph/caps.c | 143 +++++++-- fs/ceph/crypto.c | 367 +++++++++++++++++++++-- fs/ceph/crypto.h | 118 +++++++- fs/ceph/dir.c | 8 + fs/ceph/file.c | 467 +++++++++++++++++++++++++---- fs/ceph/inode.c | 282 +++++++++++++++-- fs/ceph/ioctl.c | 4 +
Quoted 9 lines
fs/ceph/mds_client.c | 9 +- fs/ceph/mds_client.h | 2 + fs/ceph/super.c | 6 + fs/ceph/super.h | 11 + include/linux/ceph/osd_client.h | 6 +- include/linux/ceph/rados.h | 4 + net/ceph/osd_client.c | 32 +- 16 files changed, 1454 insertions(+), 179 deletions(-)
The xfstests-dev tests passed and have updated the wip-fscrypt branch.
Thanks Jeff!