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