1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles extended attribute management
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/file.h>
12 #include <linux/fsnotify.h>
13 #include <linux/quotaops.h>
14 #include <linux/xattr.h>
15 #include <linux/slab.h>
18 #define CACHEFILES_COOKIE_TYPE_DATA 1
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 */
28 static const char cachefiles_xattr_cache[] =
29 XATTR_USER_PREFIX "CacheFiles.cache";
31 struct cachefiles_vol_xattr {
32 __be32 reserved; /* Reserved, should be 0 */
33 __u8 data[]; /* netfs volume coherency data */
37 * set the state xattr on a cache file
39 int cachefiles_set_object_xattr(struct cachefiles_object *object)
41 struct cachefiles_xattr *buf;
42 struct dentry *dentry;
43 struct file *file = object->file;
44 unsigned int len = object->cookie->aux_len;
49 dentry = file->f_path.dentry;
51 _enter("%x,#%d", object->debug_id, len);
53 buf = kmalloc(sizeof(struct cachefiles_xattr) + len, GFP_KERNEL);
57 buf->object_size = cpu_to_be64(object->cookie->object_size);
59 buf->type = CACHEFILES_COOKIE_TYPE_DATA;
60 buf->content = object->content_info;
61 if (test_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
62 buf->content = CACHEFILES_CONTENT_DIRTY;
64 memcpy(buf->data, fscache_get_aux(object->cookie), len);
66 ret = cachefiles_inject_write_error();
68 ret = vfs_setxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache,
69 buf, sizeof(struct cachefiles_xattr) + len, 0);
71 trace_cachefiles_vfs_error(object, file_inode(file), ret,
72 cachefiles_trace_setxattr_error);
73 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
75 cachefiles_coherency_set_fail);
77 cachefiles_io_error_obj(
79 "Failed to set xattr with error %d", ret);
81 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
83 cachefiles_coherency_set_ok);
92 * check the consistency between the backing cache and the FS-Cache cookie
94 int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
96 struct cachefiles_xattr *buf;
97 struct dentry *dentry = file->f_path.dentry;
98 unsigned int len = object->cookie->aux_len, tlen;
99 const void *p = fscache_get_aux(object->cookie);
100 enum cachefiles_coherency_trace why;
104 tlen = sizeof(struct cachefiles_xattr) + len;
105 buf = kmalloc(tlen, GFP_KERNEL);
109 xlen = cachefiles_inject_read_error();
111 xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, tlen);
114 trace_cachefiles_vfs_error(object, file_inode(file), xlen,
115 cachefiles_trace_getxattr_error);
117 cachefiles_io_error_obj(
119 "Failed to read aux with error %zd", xlen);
120 why = cachefiles_coherency_check_xattr;
121 } else if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
122 why = cachefiles_coherency_check_type;
123 } else if (memcmp(buf->data, p, len) != 0) {
124 why = cachefiles_coherency_check_aux;
125 } else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
126 why = cachefiles_coherency_check_objsize;
127 } else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
128 // TODO: Begin conflict resolution
129 pr_warn("Dirty object in cache\n");
130 why = cachefiles_coherency_check_dirty;
132 why = cachefiles_coherency_check_ok;
136 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
143 * remove the object's xattr to mark it stale
145 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
146 struct cachefiles_object *object,
147 struct dentry *dentry)
151 ret = cachefiles_inject_remove_error();
153 ret = vfs_removexattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache);
155 trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
156 cachefiles_trace_remxattr_error);
157 if (ret == -ENOENT || ret == -ENODATA)
159 else if (ret != -ENOMEM)
160 cachefiles_io_error(cache,
161 "Can't remove xattr from %lu"
163 d_backing_inode(dentry)->i_ino, -ret);
166 _leave(" = %d", ret);
171 * Stick a marker on the cache object to indicate that it's dirty.
173 void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
175 const struct cred *saved_cred;
176 struct cachefiles_object *object = cookie->cache_priv;
177 struct cachefiles_cache *cache = object->volume->cache;
179 _enter("c=%08x", object->cookie->debug_id);
181 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
182 cachefiles_begin_secure(cache, &saved_cred);
183 cachefiles_set_object_xattr(object);
184 cachefiles_end_secure(cache, saved_cred);
189 * Set the state xattr on a volume directory.
191 bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume)
193 struct cachefiles_vol_xattr *buf;
194 unsigned int len = volume->vcookie->coherency_len;
195 const void *p = volume->vcookie->coherency;
196 struct dentry *dentry = volume->dentry;
199 _enter("%x,#%d", volume->vcookie->debug_id, len);
202 buf = kmalloc(len, GFP_KERNEL);
205 buf->reserved = cpu_to_be32(0);
206 memcpy(buf->data, p, volume->vcookie->coherency_len);
208 ret = cachefiles_inject_write_error();
210 ret = vfs_setxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache,
213 trace_cachefiles_vfs_error(NULL, d_inode(dentry), ret,
214 cachefiles_trace_setxattr_error);
215 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
216 cachefiles_coherency_vol_set_fail);
219 volume->cache, "Failed to set xattr with error %d", ret);
221 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
222 cachefiles_coherency_vol_set_ok);
226 _leave(" = %d", ret);
231 * Check the consistency between the backing cache and the volume cookie.
233 int cachefiles_check_volume_xattr(struct cachefiles_volume *volume)
235 struct cachefiles_vol_xattr *buf;
236 struct dentry *dentry = volume->dentry;
237 unsigned int len = volume->vcookie->coherency_len;
238 const void *p = volume->vcookie->coherency;
239 enum cachefiles_coherency_trace why;
246 buf = kmalloc(len, GFP_KERNEL);
250 xlen = cachefiles_inject_read_error();
252 xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, len);
255 trace_cachefiles_vfs_error(NULL, d_inode(dentry), xlen,
256 cachefiles_trace_getxattr_error);
260 "Failed to read xattr with error %zd", xlen);
262 why = cachefiles_coherency_vol_check_xattr;
263 } else if (buf->reserved != cpu_to_be32(0)) {
264 why = cachefiles_coherency_vol_check_resv;
265 } else if (memcmp(buf->data, p, len - sizeof(*buf)) != 0) {
266 why = cachefiles_coherency_vol_check_cmp;
268 why = cachefiles_coherency_vol_check_ok;
272 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino, why);
274 _leave(" = %d", ret);