1 /* CacheFiles extended attribute management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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(dentry->d_inode);
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 kerror("Can't set xattr on %*.*s [%lu] (err %d)",
55 dentry->d_name.len, dentry->d_name.len,
56 dentry->d_name.name, dentry->d_inode->i_ino,
61 /* read the current type label */
62 ret = vfs_getxattr(dentry, cachefiles_xattr_cache, xtype, 3);
67 kerror("Can't read xattr on %*.*s [%lu] (err %d)",
68 dentry->d_name.len, dentry->d_name.len,
69 dentry->d_name.name, dentry->d_inode->i_ino,
74 /* check the type is what we're expecting */
78 if (xtype[0] != type[0] || xtype[1] != type[1])
88 kerror("Cache object %lu type xattr length incorrect",
89 dentry->d_inode->i_ino);
95 kerror("Cache object %*.*s [%lu] type %s not %s",
96 dentry->d_name.len, dentry->d_name.len,
97 dentry->d_name.name, dentry->d_inode->i_ino,
104 * set the state xattr on a cache file
106 int cachefiles_set_object_xattr(struct cachefiles_object *object,
107 struct cachefiles_xattr *auxdata)
109 struct dentry *dentry = object->dentry;
114 _enter("%p,#%d", object, auxdata->len);
116 /* attempt to install the cache metadata directly */
117 _debug("SET #%u", auxdata->len);
119 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
120 &auxdata->type, auxdata->len,
122 if (ret < 0 && ret != -ENOMEM)
123 cachefiles_io_error_obj(
125 "Failed to set xattr with error %d", ret);
127 _leave(" = %d", ret);
132 * update the state xattr on a cache file
134 int cachefiles_update_object_xattr(struct cachefiles_object *object,
135 struct cachefiles_xattr *auxdata)
137 struct dentry *dentry = object->dentry;
142 _enter("%p,#%d", object, auxdata->len);
144 /* attempt to install the cache metadata directly */
145 _debug("SET #%u", auxdata->len);
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 struct dentry *dentry = object->dentry;
170 ASSERT(dentry->d_inode);
171 ASSERT(object->fscache.cookie->def->check_aux);
173 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
177 auxbuf->len = vfs_getxattr(dentry, cachefiles_xattr_cache,
178 &auxbuf->type, 512 + 1);
182 if (auxbuf->type != object->fscache.cookie->def->type)
185 dlen = auxbuf->len - 1;
186 ret = fscache_check_aux(&object->fscache, &auxbuf->data, dlen);
189 if (ret != FSCACHE_CHECKAUX_OKAY)
196 * check the state xattr on a cache file
197 * - return -ESTALE if the object should be deleted
199 int cachefiles_check_object_xattr(struct cachefiles_object *object,
200 struct cachefiles_xattr *auxdata)
202 struct cachefiles_xattr *auxbuf;
203 struct dentry *dentry = object->dentry;
206 _enter("%p,#%d", object, auxdata->len);
209 ASSERT(dentry->d_inode);
211 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
213 _leave(" = -ENOMEM");
217 /* read the current type label */
218 ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
219 &auxbuf->type, 512 + 1);
222 goto stale; /* no attribute - power went off
226 goto bad_type_length;
228 cachefiles_io_error_obj(object,
229 "Can't read xattr on %lu (err %d)",
230 dentry->d_inode->i_ino, -ret);
234 /* check the on-disk object */
236 goto bad_type_length;
238 if (auxbuf->type != auxdata->type)
243 /* consult the netfs */
244 if (object->fscache.cookie->def->check_aux) {
245 enum fscache_checkaux result;
248 dlen = auxbuf->len - 1;
250 _debug("checkaux %s #%u",
251 object->fscache.cookie->def->name, dlen);
253 result = fscache_check_aux(&object->fscache,
254 &auxbuf->data, dlen);
257 /* entry okay as is */
258 case FSCACHE_CHECKAUX_OKAY:
261 /* entry requires update */
262 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
265 /* entry requires deletion */
266 case FSCACHE_CHECKAUX_OBSOLETE:
273 /* update the current label */
274 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
275 &auxdata->type, auxdata->len,
278 cachefiles_io_error_obj(object,
279 "Can't update xattr on %lu"
281 dentry->d_inode->i_ino, -ret);
291 _leave(" = %d", ret);
295 kerror("Cache object %lu xattr length incorrect",
296 dentry->d_inode->i_ino);
306 * remove the object's xattr to mark it stale
308 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
309 struct dentry *dentry)
313 ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
315 if (ret == -ENOENT || ret == -ENODATA)
317 else if (ret != -ENOMEM)
318 cachefiles_io_error(cache,
319 "Can't remove xattr from %lu"
321 dentry->d_inode->i_ino, -ret);
324 _leave(" = %d", ret);