1 /* FS-Cache interface to CacheFiles
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/mount.h>
13 #include <linux/buffer_head.h>
16 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
18 struct cachefiles_lookup_data
{
19 struct cachefiles_xattr
*auxdata
; /* auxiliary data */
20 char *key
; /* key path */
23 static int cachefiles_attr_changed(struct fscache_object
*_object
);
26 * allocate an object record for a cookie lookup and prepare the lookup data
28 static struct fscache_object
*cachefiles_alloc_object(
29 struct fscache_cache
*_cache
,
30 struct fscache_cookie
*cookie
)
32 struct cachefiles_lookup_data
*lookup_data
;
33 struct cachefiles_object
*object
;
34 struct cachefiles_cache
*cache
;
35 struct cachefiles_xattr
*auxdata
;
36 unsigned keylen
, auxlen
;
40 cache
= container_of(_cache
, struct cachefiles_cache
, cache
);
42 _enter("{%s},%p,", cache
->cache
.identifier
, cookie
);
44 lookup_data
= kmalloc(sizeof(*lookup_data
), GFP_KERNEL
);
46 goto nomem_lookup_data
;
48 /* create a new object record and a temporary leaf image */
49 object
= kmem_cache_alloc(cachefiles_object_jar
, GFP_KERNEL
);
53 ASSERTCMP(object
->backer
, ==, NULL
);
55 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
));
56 atomic_set(&object
->usage
, 1);
58 fscache_object_init(&object
->fscache
, cookie
, &cache
->cache
);
60 object
->type
= cookie
->def
->type
;
62 /* get hold of the raw key
63 * - stick the length on the front and leave space on the back for the
66 buffer
= kmalloc((2 + 512) + 3, GFP_KERNEL
);
70 keylen
= cookie
->def
->get_key(cookie
->netfs_data
, buffer
+ 2, 512);
71 ASSERTCMP(keylen
, <, 512);
73 *(uint16_t *)buffer
= keylen
;
74 ((char *)buffer
)[keylen
+ 2] = 0;
75 ((char *)buffer
)[keylen
+ 3] = 0;
76 ((char *)buffer
)[keylen
+ 4] = 0;
78 /* turn the raw key into something that can work with as a filename */
79 key
= cachefiles_cook_key(buffer
, keylen
+ 2, object
->type
);
83 /* get hold of the auxiliary data and prepend the object type */
86 if (cookie
->def
->get_aux
) {
87 auxlen
= cookie
->def
->get_aux(cookie
->netfs_data
,
89 ASSERTCMP(auxlen
, <, 511);
92 auxdata
->len
= auxlen
+ 1;
93 auxdata
->type
= cookie
->def
->type
;
95 lookup_data
->auxdata
= auxdata
;
96 lookup_data
->key
= key
;
97 object
->lookup_data
= lookup_data
;
99 _leave(" = %p [%p]", &object
->fscache
, lookup_data
);
100 return &object
->fscache
;
105 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
));
106 kmem_cache_free(cachefiles_object_jar
, object
);
107 fscache_object_destroyed(&cache
->cache
);
111 _leave(" = -ENOMEM");
112 return ERR_PTR(-ENOMEM
);
116 * attempt to look up the nominated node in this cache
118 static void cachefiles_lookup_object(struct fscache_object
*_object
)
120 struct cachefiles_lookup_data
*lookup_data
;
121 struct cachefiles_object
*parent
, *object
;
122 struct cachefiles_cache
*cache
;
123 const struct cred
*saved_cred
;
126 _enter("{OBJ%x}", _object
->debug_id
);
128 cache
= container_of(_object
->cache
, struct cachefiles_cache
, cache
);
129 parent
= container_of(_object
->parent
,
130 struct cachefiles_object
, fscache
);
131 object
= container_of(_object
, struct cachefiles_object
, fscache
);
132 lookup_data
= object
->lookup_data
;
134 ASSERTCMP(lookup_data
, !=, NULL
);
136 /* look up the key, creating any missing bits */
137 cachefiles_begin_secure(cache
, &saved_cred
);
138 ret
= cachefiles_walk_to_object(parent
, object
,
140 lookup_data
->auxdata
);
141 cachefiles_end_secure(cache
, saved_cred
);
143 /* polish off by setting the attributes of non-index files */
145 object
->fscache
.cookie
->def
->type
!= FSCACHE_COOKIE_TYPE_INDEX
)
146 cachefiles_attr_changed(&object
->fscache
);
149 printk(KERN_WARNING
"CacheFiles: Lookup failed error %d\n",
151 fscache_object_lookup_error(&object
->fscache
);
154 _leave(" [%d]", ret
);
158 * indication of lookup completion
160 static void cachefiles_lookup_complete(struct fscache_object
*_object
)
162 struct cachefiles_object
*object
;
164 object
= container_of(_object
, struct cachefiles_object
, fscache
);
166 _enter("{OBJ%x,%p}", object
->fscache
.debug_id
, object
->lookup_data
);
168 if (object
->lookup_data
) {
169 kfree(object
->lookup_data
->key
);
170 kfree(object
->lookup_data
->auxdata
);
171 kfree(object
->lookup_data
);
172 object
->lookup_data
= NULL
;
177 * increment the usage count on an inode object (may fail if unmounting)
180 struct fscache_object
*cachefiles_grab_object(struct fscache_object
*_object
)
182 struct cachefiles_object
*object
=
183 container_of(_object
, struct cachefiles_object
, fscache
);
185 _enter("{OBJ%x,%d}", _object
->debug_id
, atomic_read(&object
->usage
));
187 #ifdef CACHEFILES_DEBUG_SLAB
188 ASSERT((atomic_read(&object
->usage
) & 0xffff0000) != 0x6b6b0000);
191 atomic_inc(&object
->usage
);
192 return &object
->fscache
;
196 * update the auxilliary data for an object object on disk
198 static void cachefiles_update_object(struct fscache_object
*_object
)
200 struct cachefiles_object
*object
;
201 struct cachefiles_xattr
*auxdata
;
202 struct cachefiles_cache
*cache
;
203 struct fscache_cookie
*cookie
;
204 const struct cred
*saved_cred
;
207 _enter("{OBJ%x}", _object
->debug_id
);
209 object
= container_of(_object
, struct cachefiles_object
, fscache
);
210 cache
= container_of(object
->fscache
.cache
, struct cachefiles_cache
,
212 cookie
= object
->fscache
.cookie
;
214 if (!cookie
->def
->get_aux
) {
219 auxdata
= kmalloc(2 + 512 + 3, GFP_KERNEL
);
225 auxlen
= cookie
->def
->get_aux(cookie
->netfs_data
, auxdata
->data
, 511);
226 ASSERTCMP(auxlen
, <, 511);
228 auxdata
->len
= auxlen
+ 1;
229 auxdata
->type
= cookie
->def
->type
;
231 cachefiles_begin_secure(cache
, &saved_cred
);
232 cachefiles_update_object_xattr(object
, auxdata
);
233 cachefiles_end_secure(cache
, saved_cred
);
239 * discard the resources pinned by an object and effect retirement if
242 static void cachefiles_drop_object(struct fscache_object
*_object
)
244 struct cachefiles_object
*object
;
245 struct cachefiles_cache
*cache
;
246 const struct cred
*saved_cred
;
250 object
= container_of(_object
, struct cachefiles_object
, fscache
);
253 object
->fscache
.debug_id
, atomic_read(&object
->usage
));
255 cache
= container_of(object
->fscache
.cache
,
256 struct cachefiles_cache
, cache
);
258 #ifdef CACHEFILES_DEBUG_SLAB
259 ASSERT((atomic_read(&object
->usage
) & 0xffff0000) != 0x6b6b0000);
262 /* delete retired objects */
263 if (object
->fscache
.state
== FSCACHE_OBJECT_RECYCLING
&&
264 _object
!= cache
->cache
.fsdef
266 _debug("- retire object OBJ%x", object
->fscache
.debug_id
);
267 cachefiles_begin_secure(cache
, &saved_cred
);
268 cachefiles_delete_object(cache
, object
);
269 cachefiles_end_secure(cache
, saved_cred
);
272 /* close the filesystem stuff attached to the object */
273 if (object
->backer
!= object
->dentry
)
274 dput(object
->backer
);
275 object
->backer
= NULL
;
277 /* note that the object is now inactive */
278 if (test_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
)) {
279 write_lock(&cache
->active_lock
);
280 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE
,
283 rb_erase(&object
->active_node
, &cache
->active_nodes
);
284 wake_up_bit(&object
->flags
, CACHEFILES_OBJECT_ACTIVE
);
285 write_unlock(&cache
->active_lock
);
288 dput(object
->dentry
);
289 object
->dentry
= NULL
;
295 * dispose of a reference to an object
297 static void cachefiles_put_object(struct fscache_object
*_object
)
299 struct cachefiles_object
*object
;
300 struct fscache_cache
*cache
;
304 object
= container_of(_object
, struct cachefiles_object
, fscache
);
307 object
->fscache
.debug_id
, atomic_read(&object
->usage
));
309 #ifdef CACHEFILES_DEBUG_SLAB
310 ASSERT((atomic_read(&object
->usage
) & 0xffff0000) != 0x6b6b0000);
313 ASSERTIFCMP(object
->fscache
.parent
,
314 object
->fscache
.parent
->n_children
, >, 0);
316 if (atomic_dec_and_test(&object
->usage
)) {
317 _debug("- kill object OBJ%x", object
->fscache
.debug_id
);
319 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE
, &object
->flags
));
320 ASSERTCMP(object
->fscache
.parent
, ==, NULL
);
321 ASSERTCMP(object
->backer
, ==, NULL
);
322 ASSERTCMP(object
->dentry
, ==, NULL
);
323 ASSERTCMP(object
->fscache
.n_ops
, ==, 0);
324 ASSERTCMP(object
->fscache
.n_children
, ==, 0);
326 if (object
->lookup_data
) {
327 kfree(object
->lookup_data
->key
);
328 kfree(object
->lookup_data
->auxdata
);
329 kfree(object
->lookup_data
);
330 object
->lookup_data
= NULL
;
333 cache
= object
->fscache
.cache
;
334 kmem_cache_free(cachefiles_object_jar
, object
);
335 fscache_object_destroyed(cache
);
344 static void cachefiles_sync_cache(struct fscache_cache
*_cache
)
346 struct cachefiles_cache
*cache
;
347 const struct cred
*saved_cred
;
350 _enter("%p", _cache
);
352 cache
= container_of(_cache
, struct cachefiles_cache
, cache
);
354 /* make sure all pages pinned by operations on behalf of the netfs are
356 cachefiles_begin_secure(cache
, &saved_cred
);
357 ret
= fsync_super(cache
->mnt
->mnt_sb
);
358 cachefiles_end_secure(cache
, saved_cred
);
361 cachefiles_io_error(cache
,
362 "Attempt to sync backing fs superblock"
363 " returned error %d",
368 * notification the attributes on an object have changed
369 * - called with reads/writes excluded by FS-Cache
371 static int cachefiles_attr_changed(struct fscache_object
*_object
)
373 struct cachefiles_object
*object
;
374 struct cachefiles_cache
*cache
;
375 const struct cred
*saved_cred
;
376 struct iattr newattrs
;
381 _object
->cookie
->def
->get_attr(_object
->cookie
->netfs_data
, &ni_size
);
383 _enter("{OBJ%x},[%llu]",
384 _object
->debug_id
, (unsigned long long) ni_size
);
386 object
= container_of(_object
, struct cachefiles_object
, fscache
);
387 cache
= container_of(object
->fscache
.cache
,
388 struct cachefiles_cache
, cache
);
390 if (ni_size
== object
->i_size
)
396 ASSERT(S_ISREG(object
->backer
->d_inode
->i_mode
));
398 fscache_set_store_limit(&object
->fscache
, ni_size
);
400 oi_size
= i_size_read(object
->backer
->d_inode
);
401 if (oi_size
== ni_size
)
404 newattrs
.ia_size
= ni_size
;
405 newattrs
.ia_valid
= ATTR_SIZE
;
407 cachefiles_begin_secure(cache
, &saved_cred
);
408 mutex_lock(&object
->backer
->d_inode
->i_mutex
);
409 ret
= notify_change(object
->backer
, &newattrs
);
410 mutex_unlock(&object
->backer
->d_inode
->i_mutex
);
411 cachefiles_end_secure(cache
, saved_cred
);
414 fscache_set_store_limit(&object
->fscache
, 0);
415 cachefiles_io_error_obj(object
, "Size set failed");
419 _leave(" = %d", ret
);
424 * dissociate a cache from all the pages it was backing
426 static void cachefiles_dissociate_pages(struct fscache_cache
*cache
)
431 const struct fscache_cache_ops cachefiles_cache_ops
= {
432 .name
= "cachefiles",
433 .alloc_object
= cachefiles_alloc_object
,
434 .lookup_object
= cachefiles_lookup_object
,
435 .lookup_complete
= cachefiles_lookup_complete
,
436 .grab_object
= cachefiles_grab_object
,
437 .update_object
= cachefiles_update_object
,
438 .drop_object
= cachefiles_drop_object
,
439 .put_object
= cachefiles_put_object
,
440 .sync_cache
= cachefiles_sync_cache
,
441 .attr_changed
= cachefiles_attr_changed
,
442 .read_or_alloc_page
= cachefiles_read_or_alloc_page
,
443 .read_or_alloc_pages
= cachefiles_read_or_alloc_pages
,
444 .allocate_page
= cachefiles_allocate_page
,
445 .allocate_pages
= cachefiles_allocate_pages
,
446 .write_page
= cachefiles_write_page
,
447 .uncache_page
= cachefiles_uncache_page
,
448 .dissociate_pages
= cachefiles_dissociate_pages
,