[PATCH v4 2/2] ceph: add compression_hint mount option
From: Daolong Zhang
Date: Wed Dec 02 2026 - 22:18:36 EST
Add support for the compression_hint=<none|compressible|incompressible>
mount option to the CephFS kernel client, matching the existing RBD
compression_hint option.
When set to 'compressible' or 'incompressible', CephFS appends a
CEPH_OSD_OP_SETALLOCHINT operation with
CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE or
CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE (and zero expected object/write
sizes) after the WRITE operation(s) across page writeback, uninline,
direct I/O (including AIO retries), and sync writes.
Placing SETALLOCHINT after WRITE preserves missing-object and whiteout
semantics for WRITE's truncate handling: SETALLOCHINT cannot create an
object before WRITE evaluates its truncate sequence and truncate
boundary. This avoids extending absent objects to a non-zero truncate
boundary on classic OSDs, and avoids triggering a truncate to -1ULL on
the initial (1, -1ULL) sentinel in Crimson/BlueStore OSDs (which do not
normalize (1, -1ULL) before WRITE). On classic OSDs, PGTransaction still
lowers set_alloc_hint before buffer writes, so the compression hint
continues to apply on the first write.
When set to 'none' (the default), no CEPH_OSD_OP_SETALLOCHINT operation
is sent. The option is also displayed in /proc/mounts and can be updated
on remount while preserving the existing hint when omitted.
Link: https://tracker.ceph.com/issues/81140
Signed-off-by: Daolong Zhang <dlzhangak@xxxxxxxxxxxxxx>
---
Documentation/filesystems/ceph.rst | 13 +++++++
fs/ceph/addr.c | 62 ++++++++++++++++++++++--------
fs/ceph/file.c | 30 ++++++++++++---
fs/ceph/super.c | 51 ++++++++++++++++++++++++
fs/ceph/super.h | 1 +
5 files changed, 136 insertions(+), 21 deletions(-)
diff --git a/Documentation/filesystems/ceph.rst b/Documentation/filesystems/ceph.rst
index ee2ca0c0c654..0cfedc8645ee 100644
--- a/Documentation/filesystems/ceph.rst
+++ b/Documentation/filesystems/ceph.rst
@@ -214,6 +214,19 @@ Mount Options
of them. If an inode contains any stale file locks, read/write on the
inode is not allowed until applications release all stale file locks.
+ compression_hint=<none|compressible|incompressible>
+ Hint to the underlying OSD object store backend (e.g. BlueStore)
+ whether written data is compressible or incompressible. The default
+ is "none".
+
+ * none: do not send compression hints.
+
+ * compressible: hint that the data is compressible, enabling
+ compression when the OSD compression mode is set to passive.
+
+ * incompressible: hint that the data is incompressible, disabling
+ compression when the OSD compression mode is set to aggressive.
+
More Information
================
diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index de3431f28ba9..22d6ba119a85 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -1050,6 +1050,7 @@ static int write_folio_nounlock(struct folio *folio,
struct ceph_osd_request *req;
bool caching = ceph_is_cache_enabled(inode);
struct page *bounce_page = NULL;
+ u32 alloc_hint_flags = READ_ONCE(fsc->mount_options->alloc_hint_flags);
doutc(cl, "%llx.%llx folio %p idx %lu\n", ceph_vinop(inode), folio,
folio->index);
@@ -1111,7 +1112,9 @@ static int write_folio_nounlock(struct folio *folio,
fsc->write_congested = true;
req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
- page_off, &wlen, 0, 1, CEPH_OSD_OP_WRITE,
+ page_off, &wlen, 0,
+ alloc_hint_flags ? 2 : 1,
+ CEPH_OSD_OP_WRITE,
CEPH_OSD_FLAG_WRITE, snapc,
ceph_wbc.truncate_seq,
ceph_wbc.truncate_size, true);
@@ -1123,6 +1126,9 @@ static int write_folio_nounlock(struct folio *folio,
return PTR_ERR(req);
}
+ if (alloc_hint_flags)
+ osd_req_op_alloc_hint_init(req, 1, 0, 0, alloc_hint_flags);
+
if (wlen < len)
len = wlen;
@@ -1234,6 +1240,7 @@ static void writepages_finish(struct ceph_osd_request *req)
struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
unsigned int len = 0;
+ unsigned int num_ops = req->r_num_ops;
bool remove_page;
doutc(cl, "%llx.%llx rc %d\n", ceph_vinop(inode), rc);
@@ -1255,8 +1262,12 @@ static void writepages_finish(struct ceph_osd_request *req)
remove_page = !(ceph_caps_issued(ci) &
(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
+ if (num_ops > 1 &&
+ req->r_ops[num_ops - 1].op == CEPH_OSD_OP_SETALLOCHINT)
+ num_ops--;
+
/* clean all pages */
- for (i = 0; i < req->r_num_ops; i++) {
+ for (i = 0; i < num_ops; i++) {
if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
pr_warn_client(cl,
"%llx.%llx incorrect op %d req %p index %d tid %llu\n",
@@ -1622,10 +1633,14 @@ bool is_folio_index_contiguous(const struct ceph_writeback_ctl *ceph_wbc,
}
static inline
-bool is_num_ops_too_big(struct ceph_writeback_ctl *ceph_wbc)
+bool is_num_ops_too_big(struct ceph_fs_client *fsc,
+ struct ceph_writeback_ctl *ceph_wbc)
{
- return ceph_wbc->num_ops >=
- (ceph_wbc->from_pool ? CEPH_OSD_SLAB_OPS : CEPH_OSD_MAX_OPS);
+ unsigned int extra_ops =
+ READ_ONCE(fsc->mount_options->alloc_hint_flags) ? 1 : 0;
+
+ return ceph_wbc->num_ops + extra_ops >=
+ (ceph_wbc->from_pool ? CEPH_OSD_SLAB_OPS : CEPH_OSD_MAX_OPS);
}
static inline
@@ -1731,7 +1746,7 @@ void ceph_process_folio_batch(struct address_space *mapping,
*/
if (ceph_wbc->locked_pages &&
!is_folio_index_contiguous(ceph_wbc, folio) &&
- is_num_ops_too_big(ceph_wbc)) {
+ is_num_ops_too_big(fsc, ceph_wbc)) {
folio_unlock(folio);
break;
}
@@ -1831,6 +1846,8 @@ int ceph_submit_write(struct address_space *mapping,
struct ceph_osd_request *req = NULL;
struct folio *folio = NULL;
bool caching = ceph_is_cache_enabled(inode);
+ u32 alloc_hint_flags;
+ unsigned int hint_ops, write_ops;
u64 offset, start, end, last_len = 0;
u64 len;
unsigned int i, align;
@@ -1839,19 +1856,24 @@ int ceph_submit_write(struct address_space *mapping,
folio = ceph_fscrypt_pagecache_folio(page_folio(ceph_wbc->pages[0]));
ceph_folio_write_range(folio, &offset, &end);
len = ceph_wbc->wsize;
+ alloc_hint_flags = READ_ONCE(fsc->mount_options->alloc_hint_flags);
+ hint_ops = alloc_hint_flags ? 1 : 0;
+ write_ops = min_t(unsigned int, ceph_wbc->num_ops,
+ CEPH_OSD_MAX_OPS - hint_ops);
req = ceph_osdc_new_request(&fsc->client->osdc,
&ci->i_layout, vino,
- offset, &len, 0, ceph_wbc->num_ops,
+ offset, &len, 0, write_ops + hint_ops,
CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
ceph_wbc->snapc, ceph_wbc->truncate_seq,
ceph_wbc->truncate_size, false);
if (IS_ERR(req)) {
+ write_ops = min_t(unsigned int, ceph_wbc->num_ops,
+ CEPH_OSD_SLAB_OPS - hint_ops);
req = ceph_osdc_new_request(&fsc->client->osdc,
&ci->i_layout, vino,
offset, &len, 0,
- min(ceph_wbc->num_ops,
- CEPH_OSD_SLAB_OPS),
+ write_ops + hint_ops,
CEPH_OSD_OP_WRITE,
CEPH_OSD_FLAG_WRITE,
ceph_wbc->snapc,
@@ -1861,6 +1883,10 @@ int ceph_submit_write(struct address_space *mapping,
BUG_ON(IS_ERR(req));
}
+ if (alloc_hint_flags)
+ osd_req_op_alloc_hint_init(req, write_ops, 0, 0,
+ alloc_hint_flags);
+
folio = ceph_fscrypt_pagecache_folio(
page_folio(ceph_wbc->pages[ceph_wbc->locked_pages - 1]));
ceph_folio_write_range(folio, &start, &end);
@@ -1917,7 +1943,7 @@ int ceph_submit_write(struct address_space *mapping,
*/
if (offset + len != cur_offset) {
/* If it's full, stop here */
- if (ceph_wbc->op_idx + 1 == req->r_num_ops)
+ if (ceph_wbc->op_idx + 1 == write_ops)
break;
/* Kick off an fscache write with what we have so far. */
@@ -1985,12 +2011,12 @@ int ceph_submit_write(struct address_space *mapping,
align, ceph_wbc->from_pool, false);
osd_req_op_extent_update(req, ceph_wbc->op_idx, len);
- BUG_ON(ceph_wbc->op_idx + 1 != req->r_num_ops);
+ BUG_ON(ceph_wbc->op_idx + 1 != write_ops);
ceph_wbc->from_pool = false;
if (i < ceph_wbc->locked_pages) {
- BUG_ON(ceph_wbc->num_ops <= req->r_num_ops);
- ceph_wbc->num_ops -= req->r_num_ops;
+ BUG_ON(ceph_wbc->num_ops <= write_ops);
+ ceph_wbc->num_ops -= write_ops;
ceph_wbc->locked_pages -= i;
/* allocate new pages array for next request */
@@ -2001,7 +2027,7 @@ int ceph_submit_write(struct address_space *mapping,
memset(ceph_wbc->data_pages + i, 0,
ceph_wbc->locked_pages * sizeof(*ceph_wbc->pages));
} else {
- BUG_ON(ceph_wbc->num_ops != req->r_num_ops);
+ BUG_ON(ceph_wbc->num_ops != write_ops);
/* request message now owns the pages array */
ceph_wbc->pages = NULL;
}
@@ -3031,6 +3057,7 @@ int ceph_uninline_data(struct file *file)
struct ceph_snap_context *snapc = NULL;
u64 inline_version = CEPH_INLINE_NONE;
struct page *pages[1];
+ u32 alloc_hint_flags;
int err = 0;
u64 len;
@@ -3102,8 +3129,10 @@ int ceph_uninline_data(struct file *file)
if (err < 0)
goto out_unlock;
+ alloc_hint_flags = READ_ONCE(fsc->mount_options->alloc_hint_flags);
req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
- ceph_vino(inode), 0, &len, 1, 3,
+ ceph_vino(inode), 0, &len, 1,
+ alloc_hint_flags ? 4 : 3,
CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
snapc, ci->i_truncate_seq,
ci->i_truncate_size, false);
@@ -3137,6 +3166,9 @@ int ceph_uninline_data(struct file *file)
goto out_put_req;
}
+ if (alloc_hint_flags)
+ osd_req_op_alloc_hint_init(req, 3, 0, 0, alloc_hint_flags);
+
req->r_mtime = inode_get_mtime(inode);
ceph_osdc_start_request(&fsc->client->osdc, req);
err = ceph_osdc_wait_request(&fsc->client->osdc, req);
diff --git a/fs/ceph/file.c b/fs/ceph/file.c
index 6d56a9b2447e..b3e6da844762 100644
--- a/fs/ceph/file.c
+++ b/fs/ceph/file.c
@@ -1467,6 +1467,7 @@ static void ceph_aio_retry_work(struct work_struct *work)
struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_snap_context *snapc;
struct ceph_osd_request *req;
+ unsigned int i;
int ret;
spin_lock(&ci->i_ceph_lock);
@@ -1482,8 +1483,8 @@ static void ceph_aio_retry_work(struct work_struct *work)
}
spin_unlock(&ci->i_ceph_lock);
- req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
- false, GFP_NOFS);
+ req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc,
+ orig_req->r_num_ops, false, GFP_NOFS);
if (!req) {
ret = -ENOMEM;
req = orig_req;
@@ -1494,7 +1495,8 @@ static void ceph_aio_retry_work(struct work_struct *work)
ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
- req->r_ops[0] = orig_req->r_ops[0];
+ for (i = 0; i < orig_req->r_num_ops; i++)
+ req->r_ops[i] = orig_req->r_ops[i];
req->r_mtime = aio_req->mtime;
req->r_data_offset = req->r_ops[0].extent.offset;
@@ -1579,6 +1581,8 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
struct ceph_osd_req_op *op;
int readop = sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ;
int extent_cnt;
+ u32 alloc_hint_flags = write ?
+ READ_ONCE(fsc->mount_options->alloc_hint_flags) : 0;
if (write)
size = min_t(u64, size, fsc->mount_options->wsize);
@@ -1588,7 +1592,7 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
vino = ceph_vino(inode);
req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
vino, pos, &size, 0,
- 1,
+ alloc_hint_flags ? 2 : 1,
write ? CEPH_OSD_OP_WRITE : readop,
flags, snapc,
ci->i_truncate_seq,
@@ -1599,6 +1603,10 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
break;
}
+ if (alloc_hint_flags)
+ osd_req_op_alloc_hint_init(req, 1, 0, 0,
+ alloc_hint_flags);
+
op = &req->r_ops[0];
if (!write && sparse) {
extent_cnt = __ceph_sparse_read_ext_count(inode, size);
@@ -1811,6 +1819,8 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
bool first, last;
struct iov_iter saved_iter = *from;
size_t off;
+ u32 alloc_hint_flags;
+ unsigned int num_ops;
ceph_fscrypt_adjust_off_and_len(inode, &write_pos, &write_len);
@@ -2065,9 +2075,13 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
}
}
+ alloc_hint_flags =
+ READ_ONCE(fsc->mount_options->alloc_hint_flags);
+ num_ops = (rmw ? 2 : 1) + (alloc_hint_flags ? 1 : 0);
+
req = ceph_osdc_new_request(osdc, &ci->i_layout,
- ceph_vino(inode), write_pos, &write_len,
- rmw ? 1 : 0, rmw ? 2 : 1,
+ ceph_vino(inode), write_pos,
+ &write_len, rmw ? 1 : 0, num_ops,
CEPH_OSD_OP_WRITE,
CEPH_OSD_FLAG_WRITE,
snapc, ci->i_truncate_seq,
@@ -2102,6 +2116,10 @@ ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
}
}
+ if (alloc_hint_flags)
+ osd_req_op_alloc_hint_init(req, num_ops - 1, 0, 0,
+ alloc_hint_flags);
+
ceph_osdc_start_request(osdc, req);
ret = ceph_osdc_wait_request(osdc, req);
diff --git a/fs/ceph/super.c b/fs/ceph/super.c
index 2639c14cb757..1d6400b5fb30 100644
--- a/fs/ceph/super.c
+++ b/fs/ceph/super.c
@@ -162,6 +162,7 @@ enum {
Opt_snapdirname,
Opt_mds_namespace,
Opt_recover_session,
+ Opt_compression_hint,
Opt_source,
Opt_mon_addr,
Opt_test_dummy_encryption,
@@ -195,12 +196,27 @@ static const struct constant_table ceph_param_recover[] = {
{}
};
+enum ceph_compression_hint {
+ Opt_compression_hint_none,
+ Opt_compression_hint_compressible,
+ Opt_compression_hint_incompressible,
+};
+
+static const struct constant_table ceph_param_compression_hint[] = {
+ { "none", Opt_compression_hint_none },
+ { "compressible", Opt_compression_hint_compressible },
+ { "incompressible", Opt_compression_hint_incompressible },
+ {}
+};
+
static const struct fs_parameter_spec ceph_mount_parameters[] = {
fsparam_flag_no ("acl", Opt_acl),
fsparam_flag_no ("asyncreaddir", Opt_asyncreaddir),
fsparam_s32 ("caps_max", Opt_caps_max),
fsparam_u32 ("caps_wanted_delay_max", Opt_caps_wanted_delay_max),
fsparam_u32 ("caps_wanted_delay_min", Opt_caps_wanted_delay_min),
+ fsparam_enum ("compression_hint", Opt_compression_hint,
+ ceph_param_compression_hint),
fsparam_u32 ("write_congestion_kb", Opt_congestion_kb),
fsparam_flag_no ("copyfrom", Opt_copyfrom),
fsparam_flag_no ("dcache", Opt_dcache),
@@ -235,6 +251,7 @@ static const struct fs_parameter_spec ceph_mount_parameters[] = {
struct ceph_parse_opts_ctx {
struct ceph_options *copts;
struct ceph_mount_options *opts;
+ bool compression_hint_set;
};
/*
@@ -455,6 +472,30 @@ static int ceph_parse_mount_param(struct fs_context *fc,
else
BUG();
break;
+ case Opt_compression_hint:
+ switch (result.uint_32) {
+ case Opt_compression_hint_none:
+ fsopt->alloc_hint_flags &=
+ ~(CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE |
+ CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE);
+ break;
+ case Opt_compression_hint_compressible:
+ fsopt->alloc_hint_flags |=
+ CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE;
+ fsopt->alloc_hint_flags &=
+ ~CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE;
+ break;
+ case Opt_compression_hint_incompressible:
+ fsopt->alloc_hint_flags |=
+ CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE;
+ fsopt->alloc_hint_flags &=
+ ~CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE;
+ break;
+ default:
+ BUG();
+ }
+ pctx->compression_hint_set = true;
+ break;
case Opt_source:
if (fc->source)
return invalfc(fc, "Multiple sources specified");
@@ -714,6 +755,7 @@ static int ceph_show_options(struct seq_file *m, struct dentry *root)
{
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(root->d_sb);
struct ceph_mount_options *fsopt = fsc->mount_options;
+ u32 alloc_hint_flags = READ_ONCE(fsopt->alloc_hint_flags);
size_t pos;
int ret;
@@ -767,6 +809,11 @@ static int ceph_show_options(struct seq_file *m, struct dentry *root)
if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
seq_show_option(m, "recover_session", "clean");
+ if (alloc_hint_flags & CEPH_OSD_ALLOC_HINT_FLAG_COMPRESSIBLE)
+ seq_show_option(m, "compression_hint", "compressible");
+ else if (alloc_hint_flags & CEPH_OSD_ALLOC_HINT_FLAG_INCOMPRESSIBLE)
+ seq_show_option(m, "compression_hint", "incompressible");
+
if (!(fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS))
seq_puts(m, ",wsync");
if (fsopt->flags & CEPH_MOUNT_OPT_NOPAGECACHE)
@@ -1457,6 +1504,10 @@ static int ceph_reconfigure_fc(struct fs_context *fc)
else
ceph_clear_mount_opt(fsc, LAZYIO);
+ if (pctx->compression_hint_set)
+ WRITE_ONCE(fsc->mount_options->alloc_hint_flags,
+ fsopt->alloc_hint_flags);
+
if (strcmp_null(fsc->mount_options->mon_addr, fsopt->mon_addr)) {
kfree(fsc->mount_options->mon_addr);
fsc->mount_options->mon_addr = fsopt->mon_addr;
diff --git a/fs/ceph/super.h b/fs/ceph/super.h
index 978dd7be1778..e3fd92e86a22 100644
--- a/fs/ceph/super.h
+++ b/fs/ceph/super.h
@@ -91,6 +91,7 @@ struct ceph_mount_options {
int caps_max;
unsigned int max_readdir; /* max readdir result (entries) */
unsigned int max_readdir_bytes; /* max readdir result (bytes) */
+ u32 alloc_hint_flags; /* CEPH_OSD_ALLOC_HINT_FLAG_* */
bool new_dev_syntax;
--
2.48.1