From: David Howells <dhowells@redhat.com>
To: Christian Brauner <christian@brauner.io>
Cc: David Howells <dhowells@redhat.com>,
Paulo Alcantara <pc@manguebit.org>,
Matthew Wilcox <willy@infradead.org>,
Namjae Jeon <linkinjeon@kernel.org>,
Marc Dionne <marc.dionne@auristor.com>,
Stefan Metzmacher <metze@samba.org>,
Eric Van Hensbergen <ericvh@kernel.org>,
Dominique Martinet <asmadeus@codewreck.org>,
Ilya Dryomov <idryomov@gmail.com>,
netfs@lists.linux.dev, linux-afs@lists.infradead.org,
linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org,
ceph-devel@vger.kernel.org, v9fs@lists.linux.dev,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v12 5/5] cachefiles: Preset the state xattr when creating a new file
Date: Thu, 10 Sep 2026 23:02:40 +0100 [thread overview]
Message-ID: <20260910220242.2165023-6-dhowells@redhat.com> (raw)
In-Reply-To: <20260910220242.2165023-1-dhowells@redhat.com>
With a really small cache, cachefiles is likely to see a lot of writes
hitting ENOSPC - and this can include setxattr that sets the state xattr on
a cachefile - but we don't really want to successfully fill a cache file
only to have to scrap it because we can't set the xattr.
Instead, preset the xattr when we create the tmpfile we're going to use,
and scrap the file at that point if we get ENOSPC. Only if setxattr
succeeds do we allow data to be written to the file.
Note that there is a potential performance loss in that writes to the cache
have to be delayed until this is completed - but we do the tmpfile/setxattr
in parallel, starting when the file is opened and only have to wait once
writeback occurs.
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Paulo Alcantara <pc@manguebit.org>
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
---
fs/cachefiles/internal.h | 1 +
fs/cachefiles/namei.c | 5 ++++
fs/cachefiles/xattr.c | 59 +++++++++++++++++++++++++++++++++++++++-
3 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h
index b2605111fd56..664be64ab538 100644
--- a/fs/cachefiles/internal.h
+++ b/fs/cachefiles/internal.h
@@ -283,6 +283,7 @@ void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
/*
* xattr.c
*/
+int cachefiles_preset_object_xattr(struct cachefiles_object *object, struct file *file);
extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
extern int cachefiles_check_auxdata(struct cachefiles_object *object,
struct file *file);
diff --git a/fs/cachefiles/namei.c b/fs/cachefiles/namei.c
index ca093840e577..ef656a319ede 100644
--- a/fs/cachefiles/namei.c
+++ b/fs/cachefiles/namei.c
@@ -450,6 +450,11 @@ struct file *cachefiles_create_tmpfile(struct cachefiles_object *object)
pr_notice("Cache does not support read_iter and write_iter\n");
goto err_unuse;
}
+
+ /* Preallocate space for the xattr. */
+ ret = cachefiles_preset_object_xattr(object, file);
+ if (ret < 0)
+ goto err_unuse;
out:
cachefiles_end_secure(cache, saved_cred);
object->content_info = CACHEFILES_CONTENT_ALL;
diff --git a/fs/cachefiles/xattr.c b/fs/cachefiles/xattr.c
index 551a3b0069c2..8ebb713482e3 100644
--- a/fs/cachefiles/xattr.c
+++ b/fs/cachefiles/xattr.c
@@ -34,6 +34,57 @@ struct cachefiles_vol_xattr {
__u8 data[]; /* netfs volume coherency data */
} __packed;
+/*
+ * Preset the state xattr on a cache file to allocate space for it.
+ */
+int cachefiles_preset_object_xattr(struct cachefiles_object *object, struct file *file)
+{
+ struct cachefiles_xattr *buf;
+ struct dentry *dentry = file->f_path.dentry;
+ unsigned int len = object->cookie->aux_len;
+ int ret;
+
+ buf = kzalloc(sizeof(struct cachefiles_xattr) + min(len, sizeof(__be64)), GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ buf->type = CACHEFILES_COOKIE_TYPE_DATA;
+ buf->content = CACHEFILES_CONTENT_DIRTY;
+
+ ret = cachefiles_inject_write_error();
+ if (ret == 0) {
+ ret = mnt_want_write_file(file);
+ if (ret == 0) {
+ ret = vfs_setxattr(&nop_mnt_idmap, dentry,
+ cachefiles_xattr_cache, buf,
+ sizeof(struct cachefiles_xattr) + len, 0);
+ mnt_drop_write_file(file);
+ }
+ }
+ if (ret < 0) {
+ trace_cachefiles_vfs_error(object, file_inode(file), ret,
+ cachefiles_trace_setxattr_error);
+ trace_cachefiles_coherency(object, file_inode(file)->i_ino,
+ object->object_size,
+ buf->data, buf->content,
+ cachefiles_coherency_set_fail);
+ switch (ret) {
+ case -ENOMEM:
+ case -ENOSPC:
+ break;
+ default:
+ cachefiles_io_error_obj(
+ object,
+ "Failed to set xattr with error %d", ret);
+ break;
+ }
+ }
+
+ kfree(buf);
+ _leave(" = %d", ret);
+ return ret;
+}
+
/*
* set the state xattr on a cache file
*/
@@ -82,10 +133,16 @@ int cachefiles_set_object_xattr(struct cachefiles_object *object)
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
object_size, buf->data, buf->content,
cachefiles_coherency_set_fail);
- if (ret != -ENOMEM)
+ switch (ret) {
+ case -ENOMEM:
+ break;
+ case -ENOSPC:
+ default:
cachefiles_io_error_obj(
object,
"Failed to set xattr with error %d", ret);
+ break;
+ }
} else {
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
object_size, buf->data, buf->content,
next prev parent reply other threads:[~2026-09-10 22:03 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-10 22:02 [PATCH v12 0/5] netfs, cachefiles: Changes for next, primarily occupancy tracking-related David Howells
2026-09-10 22:02 ` [PATCH v12 1/5] cachefiles: Clean up cachefiles_do_prepare_read() David Howells
2026-09-11 20:19 ` Paulo Alcantara
2026-09-10 22:02 ` [PATCH v12 2/5] netfs, cachefiles: Add a couple of traces for write failure David Howells
2026-09-11 20:19 ` Paulo Alcantara
2026-09-10 22:02 ` [PATCH v12 3/5] cachefiles: Add a tracepoint to log insufficient space errors David Howells
2026-09-11 20:19 ` Paulo Alcantara
2026-09-10 22:02 ` [PATCH v12 4/5] cachefiles: Don't rely on backing fs storage map for most use cases David Howells
2026-09-10 22:02 ` David Howells [this message]
2026-09-11 20:21 ` [PATCH v12 5/5] cachefiles: Preset the state xattr when creating a new file Paulo Alcantara
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260910220242.2165023-6-dhowells@redhat.com \
--to=dhowells@redhat.com \
--cc=asmadeus@codewreck.org \
--cc=ceph-devel@vger.kernel.org \
--cc=christian@brauner.io \
--cc=ericvh@kernel.org \
--cc=idryomov@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-afs@lists.infradead.org \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=marc.dionne@auristor.com \
--cc=metze@samba.org \
--cc=netfs@lists.linux.dev \
--cc=pc@manguebit.org \
--cc=v9fs@lists.linux.dev \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox