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
117 * - return -ETIMEDOUT to be scheduled again
119 static int cachefiles_lookup_object(struct fscache_object *_object)
121 struct cachefiles_lookup_data *lookup_data;
122 struct cachefiles_object *parent, *object;
123 struct cachefiles_cache *cache;
124 const struct cred *saved_cred;
127 _enter("{OBJ%x}", _object->debug_id);
129 cache = container_of(_object->cache, struct cachefiles_cache, cache);
130 parent = container_of(_object->parent,
131 struct cachefiles_object, fscache);
132 object = container_of(_object, struct cachefiles_object, fscache);
133 lookup_data = object->lookup_data;
135 ASSERTCMP(lookup_data, !=, NULL);
137 /* look up the key, creating any missing bits */
138 cachefiles_begin_secure(cache, &saved_cred);
139 ret = cachefiles_walk_to_object(parent, object,
141 lookup_data->auxdata);
142 cachefiles_end_secure(cache, saved_cred);
144 /* polish off by setting the attributes of non-index files */
146 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
147 cachefiles_attr_changed(&object->fscache);
149 if (ret < 0 && ret != -ETIMEDOUT) {
150 printk(KERN_WARNING "CacheFiles: Lookup failed error %d\n",
152 fscache_object_lookup_error(&object->fscache);
155 _leave(" [%d]", ret);
160 * indication of lookup completion
162 static void cachefiles_lookup_complete(struct fscache_object *_object)
164 struct cachefiles_object *object;
166 object = container_of(_object, struct cachefiles_object, fscache);
168 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
170 if (object->lookup_data) {
171 kfree(object->lookup_data->key);
172 kfree(object->lookup_data->auxdata);
173 kfree(object->lookup_data);
174 object->lookup_data = NULL;
179 * increment the usage count on an inode object (may fail if unmounting)
182 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
184 struct cachefiles_object *object =
185 container_of(_object, struct cachefiles_object, fscache);
187 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
189 #ifdef CACHEFILES_DEBUG_SLAB
190 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
193 atomic_inc(&object->usage);
194 return &object->fscache;
198 * update the auxilliary data for an object object on disk
200 static void cachefiles_update_object(struct fscache_object *_object)
202 struct cachefiles_object *object;
203 struct cachefiles_xattr *auxdata;
204 struct cachefiles_cache *cache;
205 struct fscache_cookie *cookie;
206 const struct cred *saved_cred;
209 _enter("{OBJ%x}", _object->debug_id);
211 object = container_of(_object, struct cachefiles_object, fscache);
212 cache = container_of(object->fscache.cache, struct cachefiles_cache,
214 cookie = object->fscache.cookie;
216 if (!cookie->def->get_aux) {
221 auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
227 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
228 ASSERTCMP(auxlen, <, 511);
230 auxdata->len = auxlen + 1;
231 auxdata->type = cookie->def->type;
233 cachefiles_begin_secure(cache, &saved_cred);
234 cachefiles_update_object_xattr(object, auxdata);
235 cachefiles_end_secure(cache, saved_cred);
241 * discard the resources pinned by an object and effect retirement if
244 static void cachefiles_drop_object(struct fscache_object *_object)
246 struct cachefiles_object *object;
247 struct cachefiles_cache *cache;
248 const struct cred *saved_cred;
252 object = container_of(_object, struct cachefiles_object, fscache);
255 object->fscache.debug_id, atomic_read(&object->usage));
257 cache = container_of(object->fscache.cache,
258 struct cachefiles_cache, cache);
260 #ifdef CACHEFILES_DEBUG_SLAB
261 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
264 /* delete retired objects */
265 if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
266 _object != cache->cache.fsdef
268 _debug("- retire object OBJ%x", object->fscache.debug_id);
269 cachefiles_begin_secure(cache, &saved_cred);
270 cachefiles_delete_object(cache, object);
271 cachefiles_end_secure(cache, saved_cred);
274 /* close the filesystem stuff attached to the object */
275 if (object->backer != object->dentry)
276 dput(object->backer);
277 object->backer = NULL;
279 /* note that the object is now inactive */
280 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
281 write_lock(&cache->active_lock);
282 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
285 rb_erase(&object->active_node, &cache->active_nodes);
286 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
287 write_unlock(&cache->active_lock);
290 dput(object->dentry);
291 object->dentry = NULL;
297 * dispose of a reference to an object
299 static void cachefiles_put_object(struct fscache_object *_object)
301 struct cachefiles_object *object;
302 struct fscache_cache *cache;
306 object = container_of(_object, struct cachefiles_object, fscache);
309 object->fscache.debug_id, atomic_read(&object->usage));
311 #ifdef CACHEFILES_DEBUG_SLAB
312 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
315 ASSERTIFCMP(object->fscache.parent,
316 object->fscache.parent->n_children, >, 0);
318 if (atomic_dec_and_test(&object->usage)) {
319 _debug("- kill object OBJ%x", object->fscache.debug_id);
321 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
322 ASSERTCMP(object->fscache.parent, ==, NULL);
323 ASSERTCMP(object->backer, ==, NULL);
324 ASSERTCMP(object->dentry, ==, NULL);
325 ASSERTCMP(object->fscache.n_ops, ==, 0);
326 ASSERTCMP(object->fscache.n_children, ==, 0);
328 if (object->lookup_data) {
329 kfree(object->lookup_data->key);
330 kfree(object->lookup_data->auxdata);
331 kfree(object->lookup_data);
332 object->lookup_data = NULL;
335 cache = object->fscache.cache;
336 fscache_object_destroy(&object->fscache);
337 kmem_cache_free(cachefiles_object_jar, object);
338 fscache_object_destroyed(cache);
347 static void cachefiles_sync_cache(struct fscache_cache *_cache)
349 struct cachefiles_cache *cache;
350 const struct cred *saved_cred;
353 _enter("%p", _cache);
355 cache = container_of(_cache, struct cachefiles_cache, cache);
357 /* make sure all pages pinned by operations on behalf of the netfs are
359 cachefiles_begin_secure(cache, &saved_cred);
360 down_read(&cache->mnt->mnt_sb->s_umount);
361 ret = sync_filesystem(cache->mnt->mnt_sb);
362 up_read(&cache->mnt->mnt_sb->s_umount);
363 cachefiles_end_secure(cache, saved_cred);
366 cachefiles_io_error(cache,
367 "Attempt to sync backing fs superblock"
368 " returned error %d",
373 * notification the attributes on an object have changed
374 * - called with reads/writes excluded by FS-Cache
376 static int cachefiles_attr_changed(struct fscache_object *_object)
378 struct cachefiles_object *object;
379 struct cachefiles_cache *cache;
380 const struct cred *saved_cred;
381 struct iattr newattrs;
386 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
388 _enter("{OBJ%x},[%llu]",
389 _object->debug_id, (unsigned long long) ni_size);
391 object = container_of(_object, struct cachefiles_object, fscache);
392 cache = container_of(object->fscache.cache,
393 struct cachefiles_cache, cache);
395 if (ni_size == object->i_size)
401 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
403 fscache_set_store_limit(&object->fscache, ni_size);
405 oi_size = i_size_read(object->backer->d_inode);
406 if (oi_size == ni_size)
409 cachefiles_begin_secure(cache, &saved_cred);
410 mutex_lock(&object->backer->d_inode->i_mutex);
412 /* if there's an extension to a partial page at the end of the backing
413 * file, we need to discard the partial page so that we pick up new
415 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
416 _debug("discard tail %llx", oi_size);
417 newattrs.ia_valid = ATTR_SIZE;
418 newattrs.ia_size = oi_size & PAGE_MASK;
419 ret = notify_change(object->backer, &newattrs);
421 goto truncate_failed;
424 newattrs.ia_valid = ATTR_SIZE;
425 newattrs.ia_size = ni_size;
426 ret = notify_change(object->backer, &newattrs);
429 mutex_unlock(&object->backer->d_inode->i_mutex);
430 cachefiles_end_secure(cache, saved_cred);
433 fscache_set_store_limit(&object->fscache, 0);
434 cachefiles_io_error_obj(object, "Size set failed");
438 _leave(" = %d", ret);
443 * dissociate a cache from all the pages it was backing
445 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
450 const struct fscache_cache_ops cachefiles_cache_ops = {
451 .name = "cachefiles",
452 .alloc_object = cachefiles_alloc_object,
453 .lookup_object = cachefiles_lookup_object,
454 .lookup_complete = cachefiles_lookup_complete,
455 .grab_object = cachefiles_grab_object,
456 .update_object = cachefiles_update_object,
457 .drop_object = cachefiles_drop_object,
458 .put_object = cachefiles_put_object,
459 .sync_cache = cachefiles_sync_cache,
460 .attr_changed = cachefiles_attr_changed,
461 .read_or_alloc_page = cachefiles_read_or_alloc_page,
462 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
463 .allocate_page = cachefiles_allocate_page,
464 .allocate_pages = cachefiles_allocate_pages,
465 .write_page = cachefiles_write_page,
466 .uncache_page = cachefiles_uncache_page,
467 .dissociate_pages = cachefiles_dissociate_pages,