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>
14 #include <linux/buffer_head.h>
17 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
19 struct cachefiles_lookup_data {
20 struct cachefiles_xattr *auxdata; /* auxiliary data */
21 char *key; /* key path */
24 static int cachefiles_attr_changed(struct fscache_object *_object);
27 * allocate an object record for a cookie lookup and prepare the lookup data
29 static struct fscache_object *cachefiles_alloc_object(
30 struct fscache_cache *_cache,
31 struct fscache_cookie *cookie)
33 struct cachefiles_lookup_data *lookup_data;
34 struct cachefiles_object *object;
35 struct cachefiles_cache *cache;
36 struct cachefiles_xattr *auxdata;
37 unsigned keylen, auxlen;
41 cache = container_of(_cache, struct cachefiles_cache, cache);
43 _enter("{%s},%p,", cache->cache.identifier, cookie);
45 lookup_data = kmalloc(sizeof(*lookup_data), GFP_KERNEL);
47 goto nomem_lookup_data;
49 /* create a new object record and a temporary leaf image */
50 object = kmem_cache_alloc(cachefiles_object_jar, GFP_KERNEL);
54 ASSERTCMP(object->backer, ==, NULL);
56 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
57 atomic_set(&object->usage, 1);
59 fscache_object_init(&object->fscache, cookie, &cache->cache);
61 object->type = cookie->def->type;
63 /* get hold of the raw key
64 * - stick the length on the front and leave space on the back for the
67 buffer = kmalloc((2 + 512) + 3, GFP_KERNEL);
71 keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512);
72 ASSERTCMP(keylen, <, 512);
74 *(uint16_t *)buffer = keylen;
75 ((char *)buffer)[keylen + 2] = 0;
76 ((char *)buffer)[keylen + 3] = 0;
77 ((char *)buffer)[keylen + 4] = 0;
79 /* turn the raw key into something that can work with as a filename */
80 key = cachefiles_cook_key(buffer, keylen + 2, object->type);
84 /* get hold of the auxiliary data and prepend the object type */
87 if (cookie->def->get_aux) {
88 auxlen = cookie->def->get_aux(cookie->netfs_data,
90 ASSERTCMP(auxlen, <, 511);
93 auxdata->len = auxlen + 1;
94 auxdata->type = cookie->def->type;
96 lookup_data->auxdata = auxdata;
97 lookup_data->key = key;
98 object->lookup_data = lookup_data;
100 _leave(" = %p [%p]", &object->fscache, lookup_data);
101 return &object->fscache;
106 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
107 kmem_cache_free(cachefiles_object_jar, object);
108 fscache_object_destroyed(&cache->cache);
112 _leave(" = -ENOMEM");
113 return ERR_PTR(-ENOMEM);
117 * attempt to look up the nominated node in this cache
118 * - return -ETIMEDOUT to be scheduled again
120 static int cachefiles_lookup_object(struct fscache_object *_object)
122 struct cachefiles_lookup_data *lookup_data;
123 struct cachefiles_object *parent, *object;
124 struct cachefiles_cache *cache;
125 const struct cred *saved_cred;
128 _enter("{OBJ%x}", _object->debug_id);
130 cache = container_of(_object->cache, struct cachefiles_cache, cache);
131 parent = container_of(_object->parent,
132 struct cachefiles_object, fscache);
133 object = container_of(_object, struct cachefiles_object, fscache);
134 lookup_data = object->lookup_data;
136 ASSERTCMP(lookup_data, !=, NULL);
138 /* look up the key, creating any missing bits */
139 cachefiles_begin_secure(cache, &saved_cred);
140 ret = cachefiles_walk_to_object(parent, object,
142 lookup_data->auxdata);
143 cachefiles_end_secure(cache, saved_cred);
145 /* polish off by setting the attributes of non-index files */
147 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
148 cachefiles_attr_changed(&object->fscache);
150 if (ret < 0 && ret != -ETIMEDOUT) {
153 "CacheFiles: Lookup failed error %d\n", ret);
154 fscache_object_lookup_error(&object->fscache);
157 _leave(" [%d]", ret);
162 * indication of lookup completion
164 static void cachefiles_lookup_complete(struct fscache_object *_object)
166 struct cachefiles_object *object;
168 object = container_of(_object, struct cachefiles_object, fscache);
170 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
172 if (object->lookup_data) {
173 kfree(object->lookup_data->key);
174 kfree(object->lookup_data->auxdata);
175 kfree(object->lookup_data);
176 object->lookup_data = NULL;
181 * increment the usage count on an inode object (may fail if unmounting)
184 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
186 struct cachefiles_object *object =
187 container_of(_object, struct cachefiles_object, fscache);
189 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
191 #ifdef CACHEFILES_DEBUG_SLAB
192 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
195 atomic_inc(&object->usage);
196 return &object->fscache;
200 * update the auxiliary data for an object object on disk
202 static void cachefiles_update_object(struct fscache_object *_object)
204 struct cachefiles_object *object;
205 struct cachefiles_xattr *auxdata;
206 struct cachefiles_cache *cache;
207 struct fscache_cookie *cookie;
208 const struct cred *saved_cred;
211 _enter("{OBJ%x}", _object->debug_id);
213 object = container_of(_object, struct cachefiles_object, fscache);
214 cache = container_of(object->fscache.cache, struct cachefiles_cache,
216 cookie = object->fscache.cookie;
218 if (!cookie->def->get_aux) {
223 auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
229 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
230 ASSERTCMP(auxlen, <, 511);
232 auxdata->len = auxlen + 1;
233 auxdata->type = cookie->def->type;
235 cachefiles_begin_secure(cache, &saved_cred);
236 cachefiles_update_object_xattr(object, auxdata);
237 cachefiles_end_secure(cache, saved_cred);
243 * discard the resources pinned by an object and effect retirement if
246 static void cachefiles_drop_object(struct fscache_object *_object)
248 struct cachefiles_object *object;
249 struct cachefiles_cache *cache;
250 const struct cred *saved_cred;
254 object = container_of(_object, struct cachefiles_object, fscache);
257 object->fscache.debug_id, atomic_read(&object->usage));
259 cache = container_of(object->fscache.cache,
260 struct cachefiles_cache, cache);
262 #ifdef CACHEFILES_DEBUG_SLAB
263 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
266 /* delete retired objects */
267 if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
268 _object != cache->cache.fsdef
270 _debug("- retire object OBJ%x", object->fscache.debug_id);
271 cachefiles_begin_secure(cache, &saved_cred);
272 cachefiles_delete_object(cache, object);
273 cachefiles_end_secure(cache, saved_cred);
276 /* close the filesystem stuff attached to the object */
277 if (object->backer != object->dentry)
278 dput(object->backer);
279 object->backer = NULL;
281 /* note that the object is now inactive */
282 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
283 write_lock(&cache->active_lock);
284 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
287 rb_erase(&object->active_node, &cache->active_nodes);
288 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
289 write_unlock(&cache->active_lock);
292 dput(object->dentry);
293 object->dentry = NULL;
299 * dispose of a reference to an object
301 static void cachefiles_put_object(struct fscache_object *_object)
303 struct cachefiles_object *object;
304 struct fscache_cache *cache;
308 object = container_of(_object, struct cachefiles_object, fscache);
311 object->fscache.debug_id, atomic_read(&object->usage));
313 #ifdef CACHEFILES_DEBUG_SLAB
314 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
317 ASSERTIFCMP(object->fscache.parent,
318 object->fscache.parent->n_children, >, 0);
320 if (atomic_dec_and_test(&object->usage)) {
321 _debug("- kill object OBJ%x", object->fscache.debug_id);
323 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
324 ASSERTCMP(object->fscache.parent, ==, NULL);
325 ASSERTCMP(object->backer, ==, NULL);
326 ASSERTCMP(object->dentry, ==, NULL);
327 ASSERTCMP(object->fscache.n_ops, ==, 0);
328 ASSERTCMP(object->fscache.n_children, ==, 0);
330 if (object->lookup_data) {
331 kfree(object->lookup_data->key);
332 kfree(object->lookup_data->auxdata);
333 kfree(object->lookup_data);
334 object->lookup_data = NULL;
337 cache = object->fscache.cache;
338 fscache_object_destroy(&object->fscache);
339 kmem_cache_free(cachefiles_object_jar, object);
340 fscache_object_destroyed(cache);
349 static void cachefiles_sync_cache(struct fscache_cache *_cache)
351 struct cachefiles_cache *cache;
352 const struct cred *saved_cred;
355 _enter("%p", _cache);
357 cache = container_of(_cache, struct cachefiles_cache, cache);
359 /* make sure all pages pinned by operations on behalf of the netfs are
361 cachefiles_begin_secure(cache, &saved_cred);
362 down_read(&cache->mnt->mnt_sb->s_umount);
363 ret = sync_filesystem(cache->mnt->mnt_sb);
364 up_read(&cache->mnt->mnt_sb->s_umount);
365 cachefiles_end_secure(cache, saved_cred);
368 cachefiles_io_error(cache,
369 "Attempt to sync backing fs superblock"
370 " returned error %d",
375 * notification the attributes on an object have changed
376 * - called with reads/writes excluded by FS-Cache
378 static int cachefiles_attr_changed(struct fscache_object *_object)
380 struct cachefiles_object *object;
381 struct cachefiles_cache *cache;
382 const struct cred *saved_cred;
383 struct iattr newattrs;
388 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
390 _enter("{OBJ%x},[%llu]",
391 _object->debug_id, (unsigned long long) ni_size);
393 object = container_of(_object, struct cachefiles_object, fscache);
394 cache = container_of(object->fscache.cache,
395 struct cachefiles_cache, cache);
397 if (ni_size == object->i_size)
403 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
405 fscache_set_store_limit(&object->fscache, ni_size);
407 oi_size = i_size_read(object->backer->d_inode);
408 if (oi_size == ni_size)
411 cachefiles_begin_secure(cache, &saved_cred);
412 mutex_lock(&object->backer->d_inode->i_mutex);
414 /* if there's an extension to a partial page at the end of the backing
415 * file, we need to discard the partial page so that we pick up new
417 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
418 _debug("discard tail %llx", oi_size);
419 newattrs.ia_valid = ATTR_SIZE;
420 newattrs.ia_size = oi_size & PAGE_MASK;
421 ret = notify_change(object->backer, &newattrs);
423 goto truncate_failed;
426 newattrs.ia_valid = ATTR_SIZE;
427 newattrs.ia_size = ni_size;
428 ret = notify_change(object->backer, &newattrs);
431 mutex_unlock(&object->backer->d_inode->i_mutex);
432 cachefiles_end_secure(cache, saved_cred);
435 fscache_set_store_limit(&object->fscache, 0);
436 cachefiles_io_error_obj(object, "Size set failed");
440 _leave(" = %d", ret);
445 * dissociate a cache from all the pages it was backing
447 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
452 const struct fscache_cache_ops cachefiles_cache_ops = {
453 .name = "cachefiles",
454 .alloc_object = cachefiles_alloc_object,
455 .lookup_object = cachefiles_lookup_object,
456 .lookup_complete = cachefiles_lookup_complete,
457 .grab_object = cachefiles_grab_object,
458 .update_object = cachefiles_update_object,
459 .drop_object = cachefiles_drop_object,
460 .put_object = cachefiles_put_object,
461 .sync_cache = cachefiles_sync_cache,
462 .attr_changed = cachefiles_attr_changed,
463 .read_or_alloc_page = cachefiles_read_or_alloc_page,
464 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
465 .allocate_page = cachefiles_allocate_page,
466 .allocate_pages = cachefiles_allocate_pages,
467 .write_page = cachefiles_write_page,
468 .uncache_page = cachefiles_uncache_page,
469 .dissociate_pages = cachefiles_dissociate_pages,