1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles extended attribute management
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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 static const char cachefiles_xattr_cache[] =
19 XATTR_USER_PREFIX "CacheFiles.cache";
22 * check the type label on an object
25 int cachefiles_check_object_type(struct cachefiles_object *object)
27 struct dentry *dentry = object->dentry;
28 char type[3], xtype[3];
32 ASSERT(d_backing_inode(dentry));
34 if (!object->fscache.cookie)
37 snprintf(type, 3, "%02x", object->fscache.cookie->def->type);
39 _enter("%x{%s}", object->fscache.debug_id, type);
41 /* attempt to install a type label directly */
42 ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache, type,
45 _debug("SET"); /* we succeeded */
50 pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
51 dentry, d_backing_inode(dentry)->i_ino,
56 /* read the current type label */
57 ret = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, xtype,
63 pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
64 dentry, d_backing_inode(dentry)->i_ino,
69 /* check the type is what we're expecting */
73 if (xtype[0] != type[0] || xtype[1] != type[1])
83 pr_err("Cache object %lu type xattr length incorrect\n",
84 d_backing_inode(dentry)->i_ino);
90 pr_err("Cache object %pd [%lu] type %s not %s\n",
91 dentry, d_backing_inode(dentry)->i_ino,
98 * set the state xattr on a cache file
100 int cachefiles_set_object_xattr(struct cachefiles_object *object,
101 struct cachefiles_xattr *auxdata)
103 struct dentry *dentry = object->dentry;
108 _enter("%p,#%d", object, auxdata->len);
110 /* attempt to install the cache metadata directly */
111 _debug("SET #%u", auxdata->len);
113 clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
114 ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
115 &auxdata->type, auxdata->len, XATTR_CREATE);
116 if (ret < 0 && ret != -ENOMEM)
117 cachefiles_io_error_obj(
119 "Failed to set xattr with error %d", ret);
121 _leave(" = %d", ret);
126 * update the state xattr on a cache file
128 int cachefiles_update_object_xattr(struct cachefiles_object *object,
129 struct cachefiles_xattr *auxdata)
131 struct dentry *dentry = object->dentry;
137 _enter("%x,#%d", object->fscache.debug_id, auxdata->len);
139 /* attempt to install the cache metadata directly */
140 _debug("SET #%u", auxdata->len);
142 clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
143 ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
144 &auxdata->type, auxdata->len, XATTR_REPLACE);
145 if (ret < 0 && ret != -ENOMEM)
146 cachefiles_io_error_obj(
148 "Failed to update xattr with error %d", ret);
150 _leave(" = %d", ret);
155 * check the consistency between the backing cache and the FS-Cache cookie
157 int cachefiles_check_auxdata(struct cachefiles_object *object)
159 struct cachefiles_xattr *auxbuf;
160 enum fscache_checkaux validity;
161 struct dentry *dentry = object->dentry;
166 ASSERT(d_backing_inode(dentry));
167 ASSERT(object->fscache.cookie->def->check_aux);
169 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
173 xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
174 &auxbuf->type, 512 + 1);
177 auxbuf->type != object->fscache.cookie->def->type)
181 validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen,
182 i_size_read(d_backing_inode(dentry)));
183 if (validity != FSCACHE_CHECKAUX_OKAY)
193 * check the state xattr on a cache file
194 * - return -ESTALE if the object should be deleted
196 int cachefiles_check_object_xattr(struct cachefiles_object *object,
197 struct cachefiles_xattr *auxdata)
199 struct cachefiles_xattr *auxbuf;
200 struct dentry *dentry = object->dentry;
203 _enter("%p,#%d", object, auxdata->len);
206 ASSERT(d_backing_inode(dentry));
208 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
210 _leave(" = -ENOMEM");
214 /* read the current type label */
215 ret = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
216 &auxbuf->type, 512 + 1);
219 goto stale; /* no attribute - power went off
223 goto bad_type_length;
225 cachefiles_io_error_obj(object,
226 "Can't read xattr on %lu (err %d)",
227 d_backing_inode(dentry)->i_ino, -ret);
231 /* check the on-disk object */
233 goto bad_type_length;
235 if (auxbuf->type != auxdata->type)
240 /* consult the netfs */
241 if (object->fscache.cookie->def->check_aux) {
242 enum fscache_checkaux result;
245 dlen = auxbuf->len - 1;
247 _debug("checkaux %s #%u",
248 object->fscache.cookie->def->name, dlen);
250 result = fscache_check_aux(&object->fscache,
252 i_size_read(d_backing_inode(dentry)));
255 /* entry okay as is */
256 case FSCACHE_CHECKAUX_OKAY:
259 /* entry requires update */
260 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
263 /* entry requires deletion */
264 case FSCACHE_CHECKAUX_OBSOLETE:
271 /* update the current label */
272 ret = vfs_setxattr(&init_user_ns, dentry,
273 cachefiles_xattr_cache, &auxdata->type,
274 auxdata->len, XATTR_REPLACE);
276 cachefiles_io_error_obj(object,
277 "Can't update xattr on %lu"
279 d_backing_inode(dentry)->i_ino, -ret);
289 _leave(" = %d", ret);
293 pr_err("Cache object %lu xattr length incorrect\n",
294 d_backing_inode(dentry)->i_ino);
304 * remove the object's xattr to mark it stale
306 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
307 struct dentry *dentry)
311 ret = vfs_removexattr(&init_user_ns, dentry, cachefiles_xattr_cache);
313 if (ret == -ENOENT || ret == -ENODATA)
315 else if (ret != -ENOMEM)
316 cachefiles_io_error(cache,
317 "Can't remove xattr from %lu"
319 d_backing_inode(dentry)->i_ino, -ret);
322 _leave(" = %d", ret);