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(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 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
117 &auxdata
->type
, auxdata
->len
,
119 if (ret
< 0 && ret
!= -ENOMEM
)
120 cachefiles_io_error_obj(
122 "Failed to set xattr with error %d", ret
);
124 _leave(" = %d", ret
);
129 * update the state xattr on a cache file
131 int cachefiles_update_object_xattr(struct cachefiles_object
*object
,
132 struct cachefiles_xattr
*auxdata
)
134 struct dentry
*dentry
= object
->dentry
;
140 _enter("%p,#%d", object
, auxdata
->len
);
142 /* attempt to install the cache metadata directly */
143 _debug("SET #%u", auxdata
->len
);
145 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
146 &auxdata
->type
, auxdata
->len
,
148 if (ret
< 0 && ret
!= -ENOMEM
)
149 cachefiles_io_error_obj(
151 "Failed to update xattr with error %d", ret
);
153 _leave(" = %d", ret
);
158 * check the consistency between the backing cache and the FS-Cache cookie
160 int cachefiles_check_auxdata(struct cachefiles_object
*object
)
162 struct cachefiles_xattr
*auxbuf
;
163 enum fscache_checkaux validity
;
164 struct dentry
*dentry
= object
->dentry
;
169 ASSERT(d_backing_inode(dentry
));
170 ASSERT(object
->fscache
.cookie
->def
->check_aux
);
172 auxbuf
= kmalloc(sizeof(struct cachefiles_xattr
) + 512, GFP_KERNEL
);
176 xlen
= vfs_getxattr(dentry
, cachefiles_xattr_cache
,
177 &auxbuf
->type
, 512 + 1);
180 auxbuf
->type
!= object
->fscache
.cookie
->def
->type
)
184 validity
= fscache_check_aux(&object
->fscache
, &auxbuf
->data
, xlen
);
185 if (validity
!= FSCACHE_CHECKAUX_OKAY
)
195 * check the state xattr on a cache file
196 * - return -ESTALE if the object should be deleted
198 int cachefiles_check_object_xattr(struct cachefiles_object
*object
,
199 struct cachefiles_xattr
*auxdata
)
201 struct cachefiles_xattr
*auxbuf
;
202 struct dentry
*dentry
= object
->dentry
;
205 _enter("%p,#%d", object
, auxdata
->len
);
208 ASSERT(d_backing_inode(dentry
));
210 auxbuf
= kmalloc(sizeof(struct cachefiles_xattr
) + 512, cachefiles_gfp
);
212 _leave(" = -ENOMEM");
216 /* read the current type label */
217 ret
= vfs_getxattr(dentry
, cachefiles_xattr_cache
,
218 &auxbuf
->type
, 512 + 1);
221 goto stale
; /* no attribute - power went off
225 goto bad_type_length
;
227 cachefiles_io_error_obj(object
,
228 "Can't read xattr on %lu (err %d)",
229 d_backing_inode(dentry
)->i_ino
, -ret
);
233 /* check the on-disk object */
235 goto bad_type_length
;
237 if (auxbuf
->type
!= auxdata
->type
)
242 /* consult the netfs */
243 if (object
->fscache
.cookie
->def
->check_aux
) {
244 enum fscache_checkaux result
;
247 dlen
= auxbuf
->len
- 1;
249 _debug("checkaux %s #%u",
250 object
->fscache
.cookie
->def
->name
, dlen
);
252 result
= fscache_check_aux(&object
->fscache
,
253 &auxbuf
->data
, dlen
);
256 /* entry okay as is */
257 case FSCACHE_CHECKAUX_OKAY
:
260 /* entry requires update */
261 case FSCACHE_CHECKAUX_NEEDS_UPDATE
:
264 /* entry requires deletion */
265 case FSCACHE_CHECKAUX_OBSOLETE
:
272 /* update the current label */
273 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
274 &auxdata
->type
, auxdata
->len
,
277 cachefiles_io_error_obj(object
,
278 "Can't update xattr on %lu"
280 d_backing_inode(dentry
)->i_ino
, -ret
);
290 _leave(" = %d", ret
);
294 pr_err("Cache object %lu xattr length incorrect\n",
295 d_backing_inode(dentry
)->i_ino
);
305 * remove the object's xattr to mark it stale
307 int cachefiles_remove_object_xattr(struct cachefiles_cache
*cache
,
308 struct dentry
*dentry
)
312 ret
= vfs_removexattr(dentry
, cachefiles_xattr_cache
);
314 if (ret
== -ENOENT
|| ret
== -ENODATA
)
316 else if (ret
!= -ENOMEM
)
317 cachefiles_io_error(cache
,
318 "Can't remove xattr from %lu"
320 d_backing_inode(dentry
)->i_ino
, -ret
);
323 _leave(" = %d", ret
);