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/slab.h>
13 #include <linux/mount.h>
16 struct cachefiles_lookup_data {
17 struct cachefiles_xattr *auxdata; /* auxiliary data */
18 char *key; /* key path */
21 static int cachefiles_attr_changed(struct fscache_object *_object);
24 * allocate an object record for a cookie lookup and prepare the lookup data
26 static struct fscache_object *cachefiles_alloc_object(
27 struct fscache_cache *_cache,
28 struct fscache_cookie *cookie)
30 struct cachefiles_lookup_data *lookup_data;
31 struct cachefiles_object *object;
32 struct cachefiles_cache *cache;
33 struct cachefiles_xattr *auxdata;
34 unsigned keylen, auxlen;
38 cache = container_of(_cache, struct cachefiles_cache, cache);
40 _enter("{%s},%p,", cache->cache.identifier, cookie);
42 lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp);
44 goto nomem_lookup_data;
46 /* create a new object record and a temporary leaf image */
47 object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp);
51 ASSERTCMP(object->backer, ==, NULL);
53 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
54 atomic_set(&object->usage, 1);
56 fscache_object_init(&object->fscache, cookie, &cache->cache);
58 object->type = cookie->def->type;
60 /* get hold of the raw key
61 * - stick the length on the front and leave space on the back for the
64 buffer = kmalloc((2 + 512) + 3, cachefiles_gfp);
68 keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512);
69 ASSERTCMP(keylen, <, 512);
71 *(uint16_t *)buffer = keylen;
72 ((char *)buffer)[keylen + 2] = 0;
73 ((char *)buffer)[keylen + 3] = 0;
74 ((char *)buffer)[keylen + 4] = 0;
76 /* turn the raw key into something that can work with as a filename */
77 key = cachefiles_cook_key(buffer, keylen + 2, object->type);
81 /* get hold of the auxiliary data and prepend the object type */
84 if (cookie->def->get_aux) {
85 auxlen = cookie->def->get_aux(cookie->netfs_data,
87 ASSERTCMP(auxlen, <, 511);
90 auxdata->len = auxlen + 1;
91 auxdata->type = cookie->def->type;
93 lookup_data->auxdata = auxdata;
94 lookup_data->key = key;
95 object->lookup_data = lookup_data;
97 _leave(" = %p [%p]", &object->fscache, lookup_data);
98 return &object->fscache;
103 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
104 kmem_cache_free(cachefiles_object_jar, object);
105 fscache_object_destroyed(&cache->cache);
109 _leave(" = -ENOMEM");
110 return ERR_PTR(-ENOMEM);
114 * attempt to look up the nominated node in this cache
115 * - return -ETIMEDOUT to be scheduled again
117 static int cachefiles_lookup_object(struct fscache_object *_object)
119 struct cachefiles_lookup_data *lookup_data;
120 struct cachefiles_object *parent, *object;
121 struct cachefiles_cache *cache;
122 const struct cred *saved_cred;
125 _enter("{OBJ%x}", _object->debug_id);
127 cache = container_of(_object->cache, struct cachefiles_cache, cache);
128 parent = container_of(_object->parent,
129 struct cachefiles_object, fscache);
130 object = container_of(_object, struct cachefiles_object, fscache);
131 lookup_data = object->lookup_data;
133 ASSERTCMP(lookup_data, !=, NULL);
135 /* look up the key, creating any missing bits */
136 cachefiles_begin_secure(cache, &saved_cred);
137 ret = cachefiles_walk_to_object(parent, object,
139 lookup_data->auxdata);
140 cachefiles_end_secure(cache, saved_cred);
142 /* polish off by setting the attributes of non-index files */
144 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
145 cachefiles_attr_changed(&object->fscache);
147 if (ret < 0 && ret != -ETIMEDOUT) {
149 pr_warn("Lookup failed error %d\n", ret);
150 fscache_object_lookup_error(&object->fscache);
153 _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 auxiliary 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,
213 if (!fscache_use_cookie(_object)) {
218 cookie = object->fscache.cookie;
220 if (!cookie->def->get_aux) {
221 fscache_unuse_cookie(_object);
226 auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
228 fscache_unuse_cookie(_object);
233 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
234 fscache_unuse_cookie(_object);
235 ASSERTCMP(auxlen, <, 511);
237 auxdata->len = auxlen + 1;
238 auxdata->type = cookie->def->type;
240 cachefiles_begin_secure(cache, &saved_cred);
241 cachefiles_update_object_xattr(object, auxdata);
242 cachefiles_end_secure(cache, saved_cred);
248 * discard the resources pinned by an object and effect retirement if
251 static void cachefiles_drop_object(struct fscache_object *_object)
253 struct cachefiles_object *object;
254 struct cachefiles_cache *cache;
255 const struct cred *saved_cred;
259 object = container_of(_object, struct cachefiles_object, fscache);
262 object->fscache.debug_id, atomic_read(&object->usage));
264 cache = container_of(object->fscache.cache,
265 struct cachefiles_cache, cache);
267 #ifdef CACHEFILES_DEBUG_SLAB
268 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
271 /* delete retired objects */
272 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
273 _object != cache->cache.fsdef
275 _debug("- retire object OBJ%x", object->fscache.debug_id);
276 cachefiles_begin_secure(cache, &saved_cred);
277 cachefiles_delete_object(cache, object);
278 cachefiles_end_secure(cache, saved_cred);
281 /* close the filesystem stuff attached to the object */
282 if (object->backer != object->dentry)
283 dput(object->backer);
284 object->backer = NULL;
286 /* note that the object is now inactive */
287 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
288 write_lock(&cache->active_lock);
289 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
292 rb_erase(&object->active_node, &cache->active_nodes);
293 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
294 write_unlock(&cache->active_lock);
297 dput(object->dentry);
298 object->dentry = NULL;
304 * dispose of a reference to an object
306 static void cachefiles_put_object(struct fscache_object *_object)
308 struct cachefiles_object *object;
309 struct fscache_cache *cache;
313 object = container_of(_object, struct cachefiles_object, fscache);
316 object->fscache.debug_id, atomic_read(&object->usage));
318 #ifdef CACHEFILES_DEBUG_SLAB
319 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
322 ASSERTIFCMP(object->fscache.parent,
323 object->fscache.parent->n_children, >, 0);
325 if (atomic_dec_and_test(&object->usage)) {
326 _debug("- kill object OBJ%x", object->fscache.debug_id);
328 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
329 ASSERTCMP(object->fscache.parent, ==, NULL);
330 ASSERTCMP(object->backer, ==, NULL);
331 ASSERTCMP(object->dentry, ==, NULL);
332 ASSERTCMP(object->fscache.n_ops, ==, 0);
333 ASSERTCMP(object->fscache.n_children, ==, 0);
335 if (object->lookup_data) {
336 kfree(object->lookup_data->key);
337 kfree(object->lookup_data->auxdata);
338 kfree(object->lookup_data);
339 object->lookup_data = NULL;
342 cache = object->fscache.cache;
343 fscache_object_destroy(&object->fscache);
344 kmem_cache_free(cachefiles_object_jar, object);
345 fscache_object_destroyed(cache);
354 static void cachefiles_sync_cache(struct fscache_cache *_cache)
356 struct cachefiles_cache *cache;
357 const struct cred *saved_cred;
360 _enter("%p", _cache);
362 cache = container_of(_cache, struct cachefiles_cache, cache);
364 /* make sure all pages pinned by operations on behalf of the netfs are
366 cachefiles_begin_secure(cache, &saved_cred);
367 down_read(&cache->mnt->mnt_sb->s_umount);
368 ret = sync_filesystem(cache->mnt->mnt_sb);
369 up_read(&cache->mnt->mnt_sb->s_umount);
370 cachefiles_end_secure(cache, saved_cred);
373 cachefiles_io_error(cache,
374 "Attempt to sync backing fs superblock"
375 " returned error %d",
380 * check if the backing cache is updated to FS-Cache
381 * - called by FS-Cache when evaluates if need to invalidate the cache
383 static bool cachefiles_check_consistency(struct fscache_operation *op)
385 struct cachefiles_object *object;
386 struct cachefiles_cache *cache;
387 const struct cred *saved_cred;
390 _enter("{OBJ%x}", op->object->debug_id);
392 object = container_of(op->object, struct cachefiles_object, fscache);
393 cache = container_of(object->fscache.cache,
394 struct cachefiles_cache, cache);
396 cachefiles_begin_secure(cache, &saved_cred);
397 ret = cachefiles_check_auxdata(object);
398 cachefiles_end_secure(cache, saved_cred);
400 _leave(" = %d", ret);
405 * notification the attributes on an object have changed
406 * - called with reads/writes excluded by FS-Cache
408 static int cachefiles_attr_changed(struct fscache_object *_object)
410 struct cachefiles_object *object;
411 struct cachefiles_cache *cache;
412 const struct cred *saved_cred;
413 struct iattr newattrs;
418 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
420 _enter("{OBJ%x},[%llu]",
421 _object->debug_id, (unsigned long long) ni_size);
423 object = container_of(_object, struct cachefiles_object, fscache);
424 cache = container_of(object->fscache.cache,
425 struct cachefiles_cache, cache);
427 if (ni_size == object->i_size)
433 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
435 fscache_set_store_limit(&object->fscache, ni_size);
437 oi_size = i_size_read(object->backer->d_inode);
438 if (oi_size == ni_size)
441 cachefiles_begin_secure(cache, &saved_cred);
442 mutex_lock(&object->backer->d_inode->i_mutex);
444 /* if there's an extension to a partial page at the end of the backing
445 * file, we need to discard the partial page so that we pick up new
447 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
448 _debug("discard tail %llx", oi_size);
449 newattrs.ia_valid = ATTR_SIZE;
450 newattrs.ia_size = oi_size & PAGE_MASK;
451 ret = notify_change(object->backer, &newattrs, NULL);
453 goto truncate_failed;
456 newattrs.ia_valid = ATTR_SIZE;
457 newattrs.ia_size = ni_size;
458 ret = notify_change(object->backer, &newattrs, NULL);
461 mutex_unlock(&object->backer->d_inode->i_mutex);
462 cachefiles_end_secure(cache, saved_cred);
465 fscache_set_store_limit(&object->fscache, 0);
466 cachefiles_io_error_obj(object, "Size set failed");
470 _leave(" = %d", ret);
475 * Invalidate an object
477 static void cachefiles_invalidate_object(struct fscache_operation *op)
479 struct cachefiles_object *object;
480 struct cachefiles_cache *cache;
481 const struct cred *saved_cred;
486 object = container_of(op->object, struct cachefiles_object, fscache);
487 cache = container_of(object->fscache.cache,
488 struct cachefiles_cache, cache);
490 op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
493 _enter("{OBJ%x},[%llu]",
494 op->object->debug_id, (unsigned long long)ni_size);
496 if (object->backer) {
497 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
499 fscache_set_store_limit(&object->fscache, ni_size);
501 path.dentry = object->backer;
502 path.mnt = cache->mnt;
504 cachefiles_begin_secure(cache, &saved_cred);
505 ret = vfs_truncate(&path, 0);
507 ret = vfs_truncate(&path, ni_size);
508 cachefiles_end_secure(cache, saved_cred);
511 fscache_set_store_limit(&object->fscache, 0);
513 cachefiles_io_error_obj(object,
514 "Invalidate failed");
518 fscache_op_complete(op, true);
523 * dissociate a cache from all the pages it was backing
525 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
530 const struct fscache_cache_ops cachefiles_cache_ops = {
531 .name = "cachefiles",
532 .alloc_object = cachefiles_alloc_object,
533 .lookup_object = cachefiles_lookup_object,
534 .lookup_complete = cachefiles_lookup_complete,
535 .grab_object = cachefiles_grab_object,
536 .update_object = cachefiles_update_object,
537 .invalidate_object = cachefiles_invalidate_object,
538 .drop_object = cachefiles_drop_object,
539 .put_object = cachefiles_put_object,
540 .sync_cache = cachefiles_sync_cache,
541 .attr_changed = cachefiles_attr_changed,
542 .read_or_alloc_page = cachefiles_read_or_alloc_page,
543 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
544 .allocate_page = cachefiles_allocate_page,
545 .allocate_pages = cachefiles_allocate_pages,
546 .write_page = cachefiles_write_page,
547 .uncache_page = cachefiles_uncache_page,
548 .dissociate_pages = cachefiles_dissociate_pages,
549 .check_consistency = cachefiles_check_consistency,