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/quotaops.h>
17 #include <linux/xattr.h>
18 #include <linux/slab.h>
21 static const char cachefiles_xattr_cache
[] =
22 XATTR_USER_PREFIX
"CacheFiles.cache";
25 * check the type label on an object
28 int cachefiles_check_object_type(struct cachefiles_object
*object
)
30 struct dentry
*dentry
= object
->dentry
;
31 char type
[3], xtype
[3];
35 ASSERT(dentry
->d_inode
);
37 if (!object
->fscache
.cookie
)
40 snprintf(type
, 3, "%02x", object
->fscache
.cookie
->def
->type
);
42 _enter("%p{%s}", object
, type
);
44 /* attempt to install a type label directly */
45 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
, type
, 2,
48 _debug("SET"); /* we succeeded */
53 kerror("Can't set xattr on %*.*s [%lu] (err %d)",
54 dentry
->d_name
.len
, dentry
->d_name
.len
,
55 dentry
->d_name
.name
, dentry
->d_inode
->i_ino
,
60 /* read the current type label */
61 ret
= vfs_getxattr(dentry
, cachefiles_xattr_cache
, xtype
, 3);
66 kerror("Can't read xattr on %*.*s [%lu] (err %d)",
67 dentry
->d_name
.len
, dentry
->d_name
.len
,
68 dentry
->d_name
.name
, dentry
->d_inode
->i_ino
,
73 /* check the type is what we're expecting */
77 if (xtype
[0] != type
[0] || xtype
[1] != type
[1])
87 kerror("Cache object %lu type xattr length incorrect",
88 dentry
->d_inode
->i_ino
);
94 kerror("Cache object %*.*s [%lu] type %s not %s",
95 dentry
->d_name
.len
, dentry
->d_name
.len
,
96 dentry
->d_name
.name
, dentry
->d_inode
->i_ino
,
103 * set the state xattr on a cache file
105 int cachefiles_set_object_xattr(struct cachefiles_object
*object
,
106 struct cachefiles_xattr
*auxdata
)
108 struct dentry
*dentry
= object
->dentry
;
111 ASSERT(object
->fscache
.cookie
);
114 _enter("%p,#%d", object
, auxdata
->len
);
116 /* attempt to install the cache metadata directly */
117 _debug("SET %s #%u", object
->fscache
.cookie
->def
->name
, 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
;
140 ASSERT(object
->fscache
.cookie
);
143 _enter("%p,#%d", object
, auxdata
->len
);
145 /* attempt to install the cache metadata directly */
146 _debug("SET %s #%u", object
->fscache
.cookie
->def
->name
, auxdata
->len
);
148 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
149 &auxdata
->type
, auxdata
->len
,
151 if (ret
< 0 && ret
!= -ENOMEM
)
152 cachefiles_io_error_obj(
154 "Failed to update xattr with error %d", ret
);
156 _leave(" = %d", ret
);
161 * check the state xattr on a cache file
162 * - return -ESTALE if the object should be deleted
164 int cachefiles_check_object_xattr(struct cachefiles_object
*object
,
165 struct cachefiles_xattr
*auxdata
)
167 struct cachefiles_xattr
*auxbuf
;
168 struct dentry
*dentry
= object
->dentry
;
171 _enter("%p,#%d", object
, auxdata
->len
);
174 ASSERT(dentry
->d_inode
);
176 auxbuf
= kmalloc(sizeof(struct cachefiles_xattr
) + 512, GFP_KERNEL
);
178 _leave(" = -ENOMEM");
182 /* read the current type label */
183 ret
= vfs_getxattr(dentry
, cachefiles_xattr_cache
,
184 &auxbuf
->type
, 512 + 1);
187 goto stale
; /* no attribute - power went off
191 goto bad_type_length
;
193 cachefiles_io_error_obj(object
,
194 "Can't read xattr on %lu (err %d)",
195 dentry
->d_inode
->i_ino
, -ret
);
199 /* check the on-disk object */
201 goto bad_type_length
;
203 if (auxbuf
->type
!= auxdata
->type
)
208 /* consult the netfs */
209 if (object
->fscache
.cookie
->def
->check_aux
) {
210 enum fscache_checkaux result
;
213 dlen
= auxbuf
->len
- 1;
215 _debug("checkaux %s #%u",
216 object
->fscache
.cookie
->def
->name
, dlen
);
218 result
= fscache_check_aux(&object
->fscache
,
219 &auxbuf
->data
, dlen
);
222 /* entry okay as is */
223 case FSCACHE_CHECKAUX_OKAY
:
226 /* entry requires update */
227 case FSCACHE_CHECKAUX_NEEDS_UPDATE
:
230 /* entry requires deletion */
231 case FSCACHE_CHECKAUX_OBSOLETE
:
238 /* update the current label */
239 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
240 &auxdata
->type
, auxdata
->len
,
243 cachefiles_io_error_obj(object
,
244 "Can't update xattr on %lu"
246 dentry
->d_inode
->i_ino
, -ret
);
256 _leave(" = %d", ret
);
260 kerror("Cache object %lu xattr length incorrect",
261 dentry
->d_inode
->i_ino
);
271 * remove the object's xattr to mark it stale
273 int cachefiles_remove_object_xattr(struct cachefiles_cache
*cache
,
274 struct dentry
*dentry
)
278 ret
= vfs_removexattr(dentry
, cachefiles_xattr_cache
);
280 if (ret
== -ENOENT
|| ret
== -ENODATA
)
282 else if (ret
!= -ENOMEM
)
283 cachefiles_io_error(cache
,
284 "Can't remove xattr from %lu"
286 dentry
->d_inode
->i_ino
, -ret
);
289 _leave(" = %d", ret
);