1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles extended attribute management
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/file.h>
12 #include <linux/fsnotify.h>
13 #include <linux/quotaops.h>
14 #include <linux/xattr.h>
15 #include <linux/slab.h>
18 static const char cachefiles_xattr_cache
[] =
19 XATTR_USER_PREFIX
"CacheFiles.cache";
22 * check the type label on an object
25 int cachefiles_check_object_type(struct cachefiles_object
*object
)
27 struct dentry
*dentry
= object
->dentry
;
28 char type
[3], xtype
[3];
32 ASSERT(d_backing_inode(dentry
));
34 if (!object
->fscache
.cookie
)
37 snprintf(type
, 3, "%02x", object
->fscache
.cookie
->def
->type
);
39 _enter("%p{%s}", object
, type
);
41 /* attempt to install a type label directly */
42 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
, type
, 2,
45 _debug("SET"); /* we succeeded */
50 pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
51 dentry
, d_backing_inode(dentry
)->i_ino
,
56 /* read the current type label */
57 ret
= vfs_getxattr(dentry
, cachefiles_xattr_cache
, xtype
, 3);
62 pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
63 dentry
, d_backing_inode(dentry
)->i_ino
,
68 /* check the type is what we're expecting */
72 if (xtype
[0] != type
[0] || xtype
[1] != type
[1])
82 pr_err("Cache object %lu type xattr length incorrect\n",
83 d_backing_inode(dentry
)->i_ino
);
89 pr_err("Cache object %pd [%lu] type %s not %s\n",
90 dentry
, d_backing_inode(dentry
)->i_ino
,
97 * set the state xattr on a cache file
99 int cachefiles_set_object_xattr(struct cachefiles_object
*object
,
100 struct cachefiles_xattr
*auxdata
)
102 struct dentry
*dentry
= object
->dentry
;
107 _enter("%p,#%d", object
, auxdata
->len
);
109 /* attempt to install the cache metadata directly */
110 _debug("SET #%u", auxdata
->len
);
112 clear_bit(FSCACHE_COOKIE_AUX_UPDATED
, &object
->fscache
.cookie
->flags
);
113 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
114 &auxdata
->type
, auxdata
->len
,
116 if (ret
< 0 && ret
!= -ENOMEM
)
117 cachefiles_io_error_obj(
119 "Failed to set xattr with error %d", ret
);
121 _leave(" = %d", ret
);
126 * update the state xattr on a cache file
128 int cachefiles_update_object_xattr(struct cachefiles_object
*object
,
129 struct cachefiles_xattr
*auxdata
)
131 struct dentry
*dentry
= object
->dentry
;
137 _enter("%p,#%d", object
, auxdata
->len
);
139 /* attempt to install the cache metadata directly */
140 _debug("SET #%u", auxdata
->len
);
142 clear_bit(FSCACHE_COOKIE_AUX_UPDATED
, &object
->fscache
.cookie
->flags
);
143 ret
= vfs_setxattr(dentry
, cachefiles_xattr_cache
,
144 &auxdata
->type
, auxdata
->len
,
146 if (ret
< 0 && ret
!= -ENOMEM
)
147 cachefiles_io_error_obj(
149 "Failed to update xattr with error %d", ret
);
151 _leave(" = %d", ret
);
156 * check the consistency between the backing cache and the FS-Cache cookie
158 int cachefiles_check_auxdata(struct cachefiles_object
*object
)
160 struct cachefiles_xattr
*auxbuf
;
161 enum fscache_checkaux validity
;
162 struct dentry
*dentry
= object
->dentry
;
167 ASSERT(d_backing_inode(dentry
));
168 ASSERT(object
->fscache
.cookie
->def
->check_aux
);
170 auxbuf
= kmalloc(sizeof(struct cachefiles_xattr
) + 512, GFP_KERNEL
);
174 xlen
= vfs_getxattr(dentry
, cachefiles_xattr_cache
,
175 &auxbuf
->type
, 512 + 1);
178 auxbuf
->type
!= object
->fscache
.cookie
->def
->type
)
182 validity
= fscache_check_aux(&object
->fscache
, &auxbuf
->data
, xlen
,
183 i_size_read(d_backing_inode(dentry
)));
184 if (validity
!= FSCACHE_CHECKAUX_OKAY
)
194 * check the state xattr on a cache file
195 * - return -ESTALE if the object should be deleted
197 int cachefiles_check_object_xattr(struct cachefiles_object
*object
,
198 struct cachefiles_xattr
*auxdata
)
200 struct cachefiles_xattr
*auxbuf
;
201 struct dentry
*dentry
= object
->dentry
;
204 _enter("%p,#%d", object
, auxdata
->len
);
207 ASSERT(d_backing_inode(dentry
));
209 auxbuf
= kmalloc(sizeof(struct cachefiles_xattr
) + 512, cachefiles_gfp
);
211 _leave(" = -ENOMEM");
215 /* read the current type label */
216 ret
= vfs_getxattr(dentry
, cachefiles_xattr_cache
,
217 &auxbuf
->type
, 512 + 1);
220 goto stale
; /* no attribute - power went off
224 goto bad_type_length
;
226 cachefiles_io_error_obj(object
,
227 "Can't read xattr on %lu (err %d)",
228 d_backing_inode(dentry
)->i_ino
, -ret
);
232 /* check the on-disk object */
234 goto bad_type_length
;
236 if (auxbuf
->type
!= auxdata
->type
)
241 /* consult the netfs */
242 if (object
->fscache
.cookie
->def
->check_aux
) {
243 enum fscache_checkaux result
;
246 dlen
= auxbuf
->len
- 1;
248 _debug("checkaux %s #%u",
249 object
->fscache
.cookie
->def
->name
, dlen
);
251 result
= fscache_check_aux(&object
->fscache
,
253 i_size_read(d_backing_inode(dentry
)));
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
);