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 (dhowells@redhat.com)
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 <linux/unaligned.h>
17 #include "internal.h"
18
19 #define CACHEFILES_COOKIE_TYPE_DATA 1
20
21 struct cachefiles_xattr {
22 __be64 object_size; /* Actual size of the object */
23 __be64 zero_point; /* Size after which server has no data not written by us */
24 __u8 type; /* Type of object */
25 __u8 content; /* Content presence (enum cachefiles_content) */
26 __u8 data[]; /* netfs coherency data */
27 } __packed;
28
29 static const char cachefiles_xattr_cache[] =
30 XATTR_USER_PREFIX "CacheFiles.cache";
31
32 struct cachefiles_vol_xattr {
33 __be32 reserved; /* Reserved, should be 0 */
34 __u8 data[]; /* netfs volume coherency data */
35 } __packed;
36
37 /*
38 * set the state xattr on a cache file
39 */
cachefiles_set_object_xattr(struct cachefiles_object * object)40 int cachefiles_set_object_xattr(struct cachefiles_object *object)
41 {
42 struct cachefiles_xattr *buf;
43 struct dentry *dentry;
44 struct file *file = object->file;
45 unsigned int len = object->cookie->aux_len;
46 int ret;
47
48 if (!file)
49 return -ESTALE;
50 dentry = file->f_path.dentry;
51
52 _enter("%x,#%d", object->debug_id, len);
53
54 buf = kmalloc(sizeof(struct cachefiles_xattr) + max(len, sizeof(__be64)), GFP_KERNEL);
55 if (!buf)
56 return -ENOMEM;
57
58 buf->object_size = cpu_to_be64(object->cookie->object_size);
59 buf->zero_point = 0;
60 buf->type = CACHEFILES_COOKIE_TYPE_DATA;
61 buf->content = object->content_info;
62 if (test_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
63 buf->content = CACHEFILES_CONTENT_DIRTY;
64 put_unaligned_be64(0, (__be64 *)buf->data);
65 if (len > 0)
66 memcpy(buf->data, fscache_get_aux(object->cookie), len);
67
68 ret = cachefiles_inject_write_error();
69 if (ret == 0) {
70 ret = mnt_want_write_file(file);
71 if (ret == 0) {
72 ret = vfs_setxattr(&nop_mnt_idmap, dentry,
73 cachefiles_xattr_cache, buf,
74 sizeof(struct cachefiles_xattr) + len, 0);
75 mnt_drop_write_file(file);
76 }
77 }
78 if (ret < 0) {
79 trace_cachefiles_vfs_error(object, file_inode(file), ret,
80 cachefiles_trace_setxattr_error);
81 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
82 buf->data, buf->content,
83 cachefiles_coherency_set_fail);
84 if (ret != -ENOMEM)
85 cachefiles_io_error_obj(
86 object,
87 "Failed to set xattr with error %d", ret);
88 } else {
89 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
90 buf->data, buf->content,
91 cachefiles_coherency_set_ok);
92 }
93
94 kfree(buf);
95 _leave(" = %d", ret);
96 return ret;
97 }
98
99 /*
100 * check the consistency between the backing cache and the FS-Cache cookie
101 */
cachefiles_check_auxdata(struct cachefiles_object * object,struct file * file)102 int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
103 {
104 struct cachefiles_xattr *buf;
105 struct dentry *dentry = file->f_path.dentry;
106 unsigned int len = object->cookie->aux_len, tlen;
107 const void *p = fscache_get_aux(object->cookie);
108 enum cachefiles_coherency_trace why;
109 ssize_t xlen;
110 int ret = -ESTALE;
111
112 tlen = sizeof(struct cachefiles_xattr) + len;
113 buf = kmalloc(sizeof(struct cachefiles_xattr) + max(len, sizeof(__be64)), GFP_KERNEL);
114 if (!buf)
115 return -ENOMEM;
116 put_unaligned_be64(0, (__be64 *)buf->data);
117
118 xlen = cachefiles_inject_read_error();
119 if (xlen == 0)
120 xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, tlen);
121 if (xlen != tlen) {
122 if (xlen < 0) {
123 ret = xlen;
124 trace_cachefiles_vfs_error(object, file_inode(file), xlen,
125 cachefiles_trace_getxattr_error);
126 }
127 if (xlen == -EIO)
128 cachefiles_io_error_obj(
129 object,
130 "Failed to read aux with error %zd", xlen);
131 why = cachefiles_coherency_check_xattr;
132 goto out;
133 }
134
135 if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
136 why = cachefiles_coherency_check_type;
137 } else if (memcmp(buf->data, p, len) != 0) {
138 why = cachefiles_coherency_check_aux;
139 } else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
140 why = cachefiles_coherency_check_objsize;
141 } else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
142 // TODO: Begin conflict resolution
143 pr_warn("Dirty object in cache\n");
144 why = cachefiles_coherency_check_dirty;
145 } else {
146 why = cachefiles_coherency_check_ok;
147 ret = 0;
148 }
149
150 out:
151 trace_cachefiles_coherency(object, file_inode(file)->i_ino,
152 buf->data, buf->content, why);
153 kfree(buf);
154 return ret;
155 }
156
157 /*
158 * remove the object's xattr to mark it stale
159 */
cachefiles_remove_object_xattr(struct cachefiles_cache * cache,struct cachefiles_object * object,struct dentry * dentry)160 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
161 struct cachefiles_object *object,
162 struct dentry *dentry)
163 {
164 int ret;
165
166 ret = cachefiles_inject_remove_error();
167 if (ret == 0) {
168 ret = mnt_want_write(cache->mnt);
169 if (ret == 0) {
170 ret = vfs_removexattr(&nop_mnt_idmap, dentry,
171 cachefiles_xattr_cache);
172 mnt_drop_write(cache->mnt);
173 }
174 }
175 if (ret < 0) {
176 trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
177 cachefiles_trace_remxattr_error);
178 if (ret == -ENOENT || ret == -ENODATA)
179 ret = 0;
180 else if (ret != -ENOMEM)
181 cachefiles_io_error(cache,
182 "Can't remove xattr from %llu"
183 " (error %d)",
184 d_backing_inode(dentry)->i_ino, -ret);
185 }
186
187 _leave(" = %d", ret);
188 return ret;
189 }
190
191 /*
192 * Stick a marker on the cache object to indicate that it's dirty.
193 */
cachefiles_prepare_to_write(struct fscache_cookie * cookie)194 void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
195 {
196 const struct cred *saved_cred;
197 struct cachefiles_object *object = cookie->cache_priv;
198 struct cachefiles_cache *cache = object->volume->cache;
199
200 _enter("c=%08x", object->cookie->debug_id);
201
202 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
203 cachefiles_begin_secure(cache, &saved_cred);
204 cachefiles_set_object_xattr(object);
205 cachefiles_end_secure(cache, saved_cred);
206 }
207 }
208
209 /*
210 * Set the state xattr on a volume directory.
211 */
cachefiles_set_volume_xattr(struct cachefiles_volume * volume)212 bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume)
213 {
214 struct cachefiles_vol_xattr *buf;
215 unsigned int len = volume->vcookie->coherency_len;
216 const void *p = volume->vcookie->coherency;
217 struct dentry *dentry = volume->dentry;
218 int ret;
219
220 _enter("%x,#%d", volume->vcookie->debug_id, len);
221
222 len += sizeof(*buf);
223 buf = kmalloc(len, GFP_KERNEL);
224 if (!buf)
225 return false;
226 buf->reserved = cpu_to_be32(0);
227 memcpy(buf->data, p, volume->vcookie->coherency_len);
228
229 ret = cachefiles_inject_write_error();
230 if (ret == 0) {
231 ret = mnt_want_write(volume->cache->mnt);
232 if (ret == 0) {
233 ret = vfs_setxattr(&nop_mnt_idmap, dentry,
234 cachefiles_xattr_cache,
235 buf, len, 0);
236 mnt_drop_write(volume->cache->mnt);
237 }
238 }
239 if (ret < 0) {
240 trace_cachefiles_vfs_error(NULL, d_inode(dentry), ret,
241 cachefiles_trace_setxattr_error);
242 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
243 cachefiles_coherency_vol_set_fail);
244 if (ret != -ENOMEM)
245 cachefiles_io_error(
246 volume->cache, "Failed to set xattr with error %d", ret);
247 } else {
248 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
249 cachefiles_coherency_vol_set_ok);
250 }
251
252 kfree(buf);
253 _leave(" = %d", ret);
254 return ret == 0;
255 }
256
257 /*
258 * Check the consistency between the backing cache and the volume cookie.
259 */
cachefiles_check_volume_xattr(struct cachefiles_volume * volume)260 int cachefiles_check_volume_xattr(struct cachefiles_volume *volume)
261 {
262 struct cachefiles_vol_xattr *buf;
263 struct dentry *dentry = volume->dentry;
264 unsigned int len = volume->vcookie->coherency_len;
265 const void *p = volume->vcookie->coherency;
266 enum cachefiles_coherency_trace why;
267 ssize_t xlen;
268 int ret = -ESTALE;
269
270 _enter("");
271
272 len += sizeof(*buf);
273 buf = kmalloc(len, GFP_KERNEL);
274 if (!buf)
275 return -ENOMEM;
276
277 xlen = cachefiles_inject_read_error();
278 if (xlen == 0)
279 xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, len);
280 if (xlen != len) {
281 if (xlen < 0) {
282 ret = xlen;
283 trace_cachefiles_vfs_error(NULL, d_inode(dentry), xlen,
284 cachefiles_trace_getxattr_error);
285 if (xlen == -EIO)
286 cachefiles_io_error(
287 volume->cache,
288 "Failed to read xattr with error %zd", xlen);
289 }
290 why = cachefiles_coherency_vol_check_xattr;
291 } else if (buf->reserved != cpu_to_be32(0)) {
292 why = cachefiles_coherency_vol_check_resv;
293 } else if (memcmp(buf->data, p, len - sizeof(*buf)) != 0) {
294 why = cachefiles_coherency_vol_check_cmp;
295 } else {
296 why = cachefiles_coherency_vol_check_ok;
297 ret = 0;
298 }
299
300 trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino, why);
301 kfree(buf);
302 _leave(" = %d", ret);
303 return ret;
304 }
305