1 /* CacheFiles extended attribute management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/file.h>
16 #include <linux/fsnotify.h>
17 #include <linux/quotaops.h>
18 #include <linux/xattr.h>
19 #include <linux/slab.h>
22 static const char cachefiles_xattr_cache[] =
23 XATTR_USER_PREFIX "CacheFiles.cache";
26 * check the type label on an object
29 int cachefiles_check_object_type(struct cachefiles_object *object)
31 struct dentry *dentry = object->dentry;
32 char type[3], xtype[3];
36 ASSERT(d_backing_inode(dentry));
38 if (!object->fscache.cookie)
41 snprintf(type, 3, "%02x", object->fscache.cookie->def->type);
43 _enter("%p{%s}", object, type);
45 /* attempt to install a type label directly */
46 ret = vfs_setxattr(dentry, cachefiles_xattr_cache, type, 2,
49 _debug("SET"); /* we succeeded */
54 pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
55 dentry, d_backing_inode(dentry)->i_ino,
60 /* read the current type label */
61 ret = vfs_getxattr(dentry, cachefiles_xattr_cache, xtype, 3);
66 pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
67 dentry, d_backing_inode(dentry)->i_ino,
72 /* check the type is what we're expecting */
76 if (xtype[0] != type[0] || xtype[1] != type[1])
86 pr_err("Cache object %lu type xattr length incorrect\n",
87 d_backing_inode(dentry)->i_ino);
93 pr_err("Cache object %pd [%lu] type %s not %s\n",
94 dentry, d_backing_inode(dentry)->i_ino,
101 * set the state xattr on a cache file
103 int cachefiles_set_object_xattr(struct cachefiles_object *object,
104 struct cachefiles_xattr *auxdata)
106 struct dentry *dentry = object->dentry;
111 _enter("%p,#%d", object, auxdata->len);
113 /* attempt to install the cache metadata directly */
114 _debug("SET #%u", auxdata->len);
116 clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
117 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
118 &auxdata->type, auxdata->len,
120 if (ret < 0 && ret != -ENOMEM)
121 cachefiles_io_error_obj(
123 "Failed to set xattr with error %d", ret);
125 _leave(" = %d", ret);
130 * update the state xattr on a cache file
132 int cachefiles_update_object_xattr(struct cachefiles_object *object,
133 struct cachefiles_xattr *auxdata)
135 struct dentry *dentry = object->dentry;
141 _enter("%p,#%d", object, auxdata->len);
143 /* attempt to install the cache metadata directly */
144 _debug("SET #%u", auxdata->len);
146 clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
147 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
148 &auxdata->type, auxdata->len,
150 if (ret < 0 && ret != -ENOMEM)
151 cachefiles_io_error_obj(
153 "Failed to update xattr with error %d", ret);
155 _leave(" = %d", ret);
160 * check the consistency between the backing cache and the FS-Cache cookie
162 int cachefiles_check_auxdata(struct cachefiles_object *object)
164 struct cachefiles_xattr *auxbuf;
165 enum fscache_checkaux validity;
166 struct dentry *dentry = object->dentry;
171 ASSERT(d_backing_inode(dentry));
172 ASSERT(object->fscache.cookie->def->check_aux);
174 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
178 xlen = vfs_getxattr(dentry, cachefiles_xattr_cache,
179 &auxbuf->type, 512 + 1);
182 auxbuf->type != object->fscache.cookie->def->type)
186 validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen,
187 i_size_read(d_backing_inode(dentry)));
188 if (validity != FSCACHE_CHECKAUX_OKAY)
198 * check the state xattr on a cache file
199 * - return -ESTALE if the object should be deleted
201 int cachefiles_check_object_xattr(struct cachefiles_object *object,
202 struct cachefiles_xattr *auxdata)
204 struct cachefiles_xattr *auxbuf;
205 struct dentry *dentry = object->dentry;
208 _enter("%p,#%d", object, auxdata->len);
211 ASSERT(d_backing_inode(dentry));
213 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
215 _leave(" = -ENOMEM");
219 /* read the current type label */
220 ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
221 &auxbuf->type, 512 + 1);
224 goto stale; /* no attribute - power went off
228 goto bad_type_length;
230 cachefiles_io_error_obj(object,
231 "Can't read xattr on %lu (err %d)",
232 d_backing_inode(dentry)->i_ino, -ret);
236 /* check the on-disk object */
238 goto bad_type_length;
240 if (auxbuf->type != auxdata->type)
245 /* consult the netfs */
246 if (object->fscache.cookie->def->check_aux) {
247 enum fscache_checkaux result;
250 dlen = auxbuf->len - 1;
252 _debug("checkaux %s #%u",
253 object->fscache.cookie->def->name, dlen);
255 result = fscache_check_aux(&object->fscache,
257 i_size_read(d_backing_inode(dentry)));
260 /* entry okay as is */
261 case FSCACHE_CHECKAUX_OKAY:
264 /* entry requires update */
265 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
268 /* entry requires deletion */
269 case FSCACHE_CHECKAUX_OBSOLETE:
276 /* update the current label */
277 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
278 &auxdata->type, auxdata->len,
281 cachefiles_io_error_obj(object,
282 "Can't update xattr on %lu"
284 d_backing_inode(dentry)->i_ino, -ret);
294 _leave(" = %d", ret);
298 pr_err("Cache object %lu xattr length incorrect\n",
299 d_backing_inode(dentry)->i_ino);
309 * remove the object's xattr to mark it stale
311 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
312 struct dentry *dentry)
316 ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
318 if (ret == -ENOENT || ret == -ENODATA)
320 else if (ret != -ENOMEM)
321 cachefiles_io_error(cache,
322 "Can't remove xattr from %lu"
324 d_backing_inode(dentry)->i_ino, -ret);
327 _leave(" = %d", ret);