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;
112 ASSERT(object->fscache.cookie);
115 _enter("%p,#%d", object, auxdata->len);
117 /* attempt to install the cache metadata directly */
118 _debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len);
120 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
121 &auxdata->type, auxdata->len,
123 if (ret < 0 && ret != -ENOMEM)
124 cachefiles_io_error_obj(
126 "Failed to set xattr with error %d", ret);
128 _leave(" = %d", ret);
133 * update the state xattr on a cache file
135 int cachefiles_update_object_xattr(struct cachefiles_object *object,
136 struct cachefiles_xattr *auxdata)
138 struct dentry *dentry = object->dentry;
141 ASSERT(object->fscache.cookie);
144 _enter("%p,#%d", object, auxdata->len);
146 /* attempt to install the cache metadata directly */
147 _debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len);
149 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
150 &auxdata->type, auxdata->len,
152 if (ret < 0 && ret != -ENOMEM)
153 cachefiles_io_error_obj(
155 "Failed to update xattr with error %d", ret);
157 _leave(" = %d", ret);
162 * check the state xattr on a cache file
163 * - return -ESTALE if the object should be deleted
165 int cachefiles_check_object_xattr(struct cachefiles_object *object,
166 struct cachefiles_xattr *auxdata)
168 struct cachefiles_xattr *auxbuf;
169 struct dentry *dentry = object->dentry;
172 _enter("%p,#%d", object, auxdata->len);
175 ASSERT(dentry->d_inode);
177 auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
179 _leave(" = -ENOMEM");
183 /* read the current type label */
184 ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
185 &auxbuf->type, 512 + 1);
188 goto stale; /* no attribute - power went off
192 goto bad_type_length;
194 cachefiles_io_error_obj(object,
195 "Can't read xattr on %lu (err %d)",
196 dentry->d_inode->i_ino, -ret);
200 /* check the on-disk object */
202 goto bad_type_length;
204 if (auxbuf->type != auxdata->type)
209 /* consult the netfs */
210 if (object->fscache.cookie->def->check_aux) {
211 enum fscache_checkaux result;
214 dlen = auxbuf->len - 1;
216 _debug("checkaux %s #%u",
217 object->fscache.cookie->def->name, dlen);
219 result = fscache_check_aux(&object->fscache,
220 &auxbuf->data, dlen);
223 /* entry okay as is */
224 case FSCACHE_CHECKAUX_OKAY:
227 /* entry requires update */
228 case FSCACHE_CHECKAUX_NEEDS_UPDATE:
231 /* entry requires deletion */
232 case FSCACHE_CHECKAUX_OBSOLETE:
239 /* update the current label */
240 ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
241 &auxdata->type, auxdata->len,
244 cachefiles_io_error_obj(object,
245 "Can't update xattr on %lu"
247 dentry->d_inode->i_ino, -ret);
257 _leave(" = %d", ret);
261 kerror("Cache object %lu xattr length incorrect",
262 dentry->d_inode->i_ino);
272 * remove the object's xattr to mark it stale
274 int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
275 struct dentry *dentry)
279 ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
281 if (ret == -ENOENT || ret == -ENODATA)
283 else if (ret != -ENOMEM)
284 cachefiles_io_error(cache,
285 "Can't remove xattr from %lu"
287 dentry->d_inode->i_ino, -ret);
290 _leave(" = %d", ret);