Skip to content

Commit 72b9578

Browse files
committed
cachefiles: Implement metadata/coherency data storage in xattrs
Use an xattr on each backing file in the cache to store some metadata, such as the content type and the coherency data. Five content types are defined: (0) No content stored. (1) The file contains a single monolithic blob and must be all or nothing. This would be used for something like an AFS directory or a symlink. (2) The file is populated with content completely up to a point with nothing beyond that. (3) The file has a map attached and is sparsely populated. This would be stored in one or more additional xattrs. (4) The file is dirty, being in the process of local modification and the contents are not necessarily represented correctly by the metadata. The file should be deleted if this is seen on binding. Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeff Layton <[email protected]> cc: [email protected] Link: https://lore.kernel.org/r/163819641320.215744.16346770087799536862.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/163906942248.143852.5423738045012094252.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/163967151734.1823006.9301249989443622576.stgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/164021550471.640689.553853918307994335.stgit@warthog.procyon.org.uk/ # v4
1 parent 5d43946 commit 72b9578

File tree

4 files changed

+260
-1
lines changed

4 files changed

+260
-1
lines changed

fs/cachefiles/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ cachefiles-y := \
1111
main.o \
1212
namei.o \
1313
security.o \
14-
volume.o
14+
volume.o \
15+
xattr.o
1516

1617
cachefiles-$(CONFIG_CACHEFILES_ERROR_INJECTION) += error_inject.o
1718

fs/cachefiles/internal.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct cachefiles_object {
5555
u8 d_name_len; /* Length of filename */
5656
enum cachefiles_content content_info:8; /* Info about content presence */
5757
unsigned long flags;
58+
#define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
5859
};
5960

6061
/*
@@ -219,6 +220,17 @@ void cachefiles_acquire_volume(struct fscache_volume *volume);
219220
void cachefiles_free_volume(struct fscache_volume *volume);
220221
void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
221222

223+
/*
224+
* xattr.c
225+
*/
226+
extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
227+
extern int cachefiles_check_auxdata(struct cachefiles_object *object,
228+
struct file *file);
229+
extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
230+
struct cachefiles_object *object,
231+
struct dentry *dentry);
232+
extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
233+
222234
/*
223235
* Error handling
224236
*/
@@ -229,6 +241,15 @@ do { \
229241
set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
230242
} while (0)
231243

244+
#define cachefiles_io_error_obj(object, FMT, ...) \
245+
do { \
246+
struct cachefiles_cache *___cache; \
247+
\
248+
___cache = (object)->volume->cache; \
249+
cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
250+
(object)->debug_id); \
251+
} while (0)
252+
232253

233254
/*
234255
* Debug tracing

fs/cachefiles/xattr.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/* CacheFiles extended attribute management
3+
*
4+
* Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5+
* Written by David Howells ([email protected])
6+
*/
7+
8+
#include <linux/module.h>
9+
#include <linux/sched.h>
10+
#include <linux/file.h>
11+
#include <linux/fs.h>
12+
#include <linux/fsnotify.h>
13+
#include <linux/quotaops.h>
14+
#include <linux/xattr.h>
15+
#include <linux/slab.h>
16+
#include "internal.h"
17+
18+
#define CACHEFILES_COOKIE_TYPE_DATA 1
19+
20+
struct cachefiles_xattr {
21+
__be64 object_size; /* Actual size of the object */
22+
__be64 zero_point; /* Size after which server has no data not written by us */
23+
__u8 type; /* Type of object */
24+
__u8 content; /* Content presence (enum cachefiles_content) */
25+
__u8 data[]; /* netfs coherency data */
26+
} __packed;
27+
28+
static const char cachefiles_xattr_cache[] =
29+
XATTR_USER_PREFIX "CacheFiles.cache";
30+
31+
/*
32+
* set the state xattr on a cache file
33+
*/
34+
int cachefiles_set_object_xattr(struct cachefiles_object *object)
35+
{
36+
struct cachefiles_xattr *buf;
37+
struct dentry *dentry;
38+
struct file *file = object->file;
39+
unsigned int len = object->cookie->aux_len;
40+
int ret;
41+
42+
if (!file)
43+
return -ESTALE;
44+
dentry = file->f_path.dentry;
45+
46+
_enter("%x,#%d", object->debug_id, len);
47+
48+
buf = kmalloc(sizeof(struct cachefiles_xattr) + len, GFP_KERNEL);
49+
if (!buf)
50+
return -ENOMEM;
51+
52+
buf->object_size = cpu_to_be64(object->cookie->object_size);
53+
buf->zero_point = 0;
54+
buf->type = CACHEFILES_COOKIE_TYPE_DATA;
55+
buf->content = object->content_info;
56+
if (test_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
57+
buf->content = CACHEFILES_CONTENT_DIRTY;
58+
if (len > 0)
59+
memcpy(buf->data, fscache_get_aux(object->cookie), len);
60+
61+
ret = cachefiles_inject_write_error();
62+
if (ret == 0)
63+
ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
64+
buf, sizeof(struct cachefiles_xattr) + len, 0);
65+
if (ret < 0) {
66+
trace_cachefiles_vfs_error(object, file_inode(file), ret,
67+
cachefiles_trace_setxattr_error);
68+
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
69+
buf->content,
70+
cachefiles_coherency_set_fail);
71+
if (ret != -ENOMEM)
72+
cachefiles_io_error_obj(
73+
object,
74+
"Failed to set xattr with error %d", ret);
75+
} else {
76+
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
77+
buf->content,
78+
cachefiles_coherency_set_ok);
79+
}
80+
81+
kfree(buf);
82+
_leave(" = %d", ret);
83+
return ret;
84+
}
85+
86+
/*
87+
* check the consistency between the backing cache and the FS-Cache cookie
88+
*/
89+
int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
90+
{
91+
struct cachefiles_xattr *buf;
92+
struct dentry *dentry = file->f_path.dentry;
93+
unsigned int len = object->cookie->aux_len, tlen;
94+
const void *p = fscache_get_aux(object->cookie);
95+
enum cachefiles_coherency_trace why;
96+
ssize_t xlen;
97+
int ret = -ESTALE;
98+
99+
tlen = sizeof(struct cachefiles_xattr) + len;
100+
buf = kmalloc(tlen, GFP_KERNEL);
101+
if (!buf)
102+
return -ENOMEM;
103+
104+
xlen = cachefiles_inject_read_error();
105+
if (xlen == 0)
106+
xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, buf, tlen);
107+
if (xlen != tlen) {
108+
if (xlen < 0)
109+
trace_cachefiles_vfs_error(object, file_inode(file), xlen,
110+
cachefiles_trace_getxattr_error);
111+
if (xlen == -EIO)
112+
cachefiles_io_error_obj(
113+
object,
114+
"Failed to read aux with error %zd", xlen);
115+
why = cachefiles_coherency_check_xattr;
116+
} else if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
117+
why = cachefiles_coherency_check_type;
118+
} else if (memcmp(buf->data, p, len) != 0) {
119+
why = cachefiles_coherency_check_aux;
120+
} else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
121+
why = cachefiles_coherency_check_objsize;
122+
} else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
123+
// TODO: Begin conflict resolution
124+
pr_warn("Dirty object in cache\n");
125+
why = cachefiles_coherency_check_dirty;
126+
} else {
127+
why = cachefiles_coherency_check_ok;
128+
ret = 0;
129+
}
130+
131+
trace_cachefiles_coherency(object, file_inode(file)->i_ino,
132+
buf->content, why);
133+
kfree(buf);
134+
return ret;
135+
}
136+
137+
/*
138+
* remove the object's xattr to mark it stale
139+
*/
140+
int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
141+
struct cachefiles_object *object,
142+
struct dentry *dentry)
143+
{
144+
int ret;
145+
146+
ret = cachefiles_inject_remove_error();
147+
if (ret == 0)
148+
ret = vfs_removexattr(&init_user_ns, dentry, cachefiles_xattr_cache);
149+
if (ret < 0) {
150+
trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
151+
cachefiles_trace_remxattr_error);
152+
if (ret == -ENOENT || ret == -ENODATA)
153+
ret = 0;
154+
else if (ret != -ENOMEM)
155+
cachefiles_io_error(cache,
156+
"Can't remove xattr from %lu"
157+
" (error %d)",
158+
d_backing_inode(dentry)->i_ino, -ret);
159+
}
160+
161+
_leave(" = %d", ret);
162+
return ret;
163+
}
164+
165+
/*
166+
* Stick a marker on the cache object to indicate that it's dirty.
167+
*/
168+
void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
169+
{
170+
const struct cred *saved_cred;
171+
struct cachefiles_object *object = cookie->cache_priv;
172+
struct cachefiles_cache *cache = object->volume->cache;
173+
174+
_enter("c=%08x", object->cookie->debug_id);
175+
176+
if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
177+
cachefiles_begin_secure(cache, &saved_cred);
178+
cachefiles_set_object_xattr(object);
179+
cachefiles_end_secure(cache, saved_cred);
180+
}
181+
}

include/trace/events/cachefiles.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ enum fscache_why_object_killed {
4242
FSCACHE_OBJECT_WAS_CULLED,
4343
};
4444

45+
enum cachefiles_coherency_trace {
46+
cachefiles_coherency_check_aux,
47+
cachefiles_coherency_check_content,
48+
cachefiles_coherency_check_dirty,
49+
cachefiles_coherency_check_len,
50+
cachefiles_coherency_check_objsize,
51+
cachefiles_coherency_check_ok,
52+
cachefiles_coherency_check_type,
53+
cachefiles_coherency_check_xattr,
54+
cachefiles_coherency_set_fail,
55+
cachefiles_coherency_set_ok,
56+
};
57+
4558
enum cachefiles_trunc_trace {
4659
cachefiles_trunc_dio_adjust,
4760
cachefiles_trunc_expand_tmpfile,
@@ -95,6 +108,18 @@ enum cachefiles_error_trace {
95108
EM(cachefiles_obj_see_withdraw_cookie, "SEE withdraw_cookie") \
96109
E_(cachefiles_obj_see_withdrawal, "SEE withdrawal")
97110

111+
#define cachefiles_coherency_traces \
112+
EM(cachefiles_coherency_check_aux, "BAD aux ") \
113+
EM(cachefiles_coherency_check_content, "BAD cont") \
114+
EM(cachefiles_coherency_check_dirty, "BAD dirt") \
115+
EM(cachefiles_coherency_check_len, "BAD len ") \
116+
EM(cachefiles_coherency_check_objsize, "BAD osiz") \
117+
EM(cachefiles_coherency_check_ok, "OK ") \
118+
EM(cachefiles_coherency_check_type, "BAD type") \
119+
EM(cachefiles_coherency_check_xattr, "BAD xatt") \
120+
EM(cachefiles_coherency_set_fail, "SET fail") \
121+
E_(cachefiles_coherency_set_ok, "SET ok ")
122+
98123
#define cachefiles_trunc_traces \
99124
EM(cachefiles_trunc_dio_adjust, "DIOADJ") \
100125
EM(cachefiles_trunc_expand_tmpfile, "EXPTMP") \
@@ -130,6 +155,7 @@ enum cachefiles_error_trace {
130155

131156
cachefiles_obj_kill_traces;
132157
cachefiles_obj_ref_traces;
158+
cachefiles_coherency_traces;
133159
cachefiles_trunc_traces;
134160
cachefiles_error_traces;
135161

@@ -287,6 +313,36 @@ TRACE_EVENT(cachefiles_rename,
287313
__print_symbolic(__entry->why, cachefiles_obj_kill_traces))
288314
);
289315

316+
TRACE_EVENT(cachefiles_coherency,
317+
TP_PROTO(struct cachefiles_object *obj,
318+
ino_t ino,
319+
enum cachefiles_content content,
320+
enum cachefiles_coherency_trace why),
321+
322+
TP_ARGS(obj, ino, content, why),
323+
324+
/* Note that obj may be NULL */
325+
TP_STRUCT__entry(
326+
__field(unsigned int, obj )
327+
__field(enum cachefiles_coherency_trace, why )
328+
__field(enum cachefiles_content, content )
329+
__field(u64, ino )
330+
),
331+
332+
TP_fast_assign(
333+
__entry->obj = obj->debug_id;
334+
__entry->why = why;
335+
__entry->content = content;
336+
__entry->ino = ino;
337+
),
338+
339+
TP_printk("o=%08x %s i=%llx c=%u",
340+
__entry->obj,
341+
__print_symbolic(__entry->why, cachefiles_coherency_traces),
342+
__entry->ino,
343+
__entry->content)
344+
);
345+
290346
TRACE_EVENT(cachefiles_trunc,
291347
TP_PROTO(struct cachefiles_object *obj, struct inode *backer,
292348
loff_t from, loff_t to, enum cachefiles_trunc_trace why),

0 commit comments

Comments
 (0)