Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / fs / cachefiles / interface.c
blob67bef6d01484a1b058f6513fce1277db72903650
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 "internal.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;
37 void *buffer;
38 char *key;
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);
45 if (!lookup_data)
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);
50 if (!object)
51 goto nomem_object;
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
64 * encoder
66 buffer = kmalloc((2 + 512) + 3, GFP_KERNEL);
67 if (!buffer)
68 goto nomem_buffer;
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);
80 if (!key)
81 goto nomem_key;
83 /* get hold of the auxiliary data and prepend the object type */
84 auxdata = buffer;
85 auxlen = 0;
86 if (cookie->def->get_aux) {
87 auxlen = cookie->def->get_aux(cookie->netfs_data,
88 auxdata->data, 511);
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;
102 nomem_key:
103 kfree(buffer);
104 nomem_buffer:
105 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
106 kmem_cache_free(cachefiles_object_jar, object);
107 fscache_object_destroyed(&cache->cache);
108 nomem_object:
109 kfree(lookup_data);
110 nomem_lookup_data:
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;
125 int ret;
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,
140 lookup_data->key,
141 lookup_data->auxdata);
142 cachefiles_end_secure(cache, saved_cred);
144 /* polish off by setting the attributes of non-index files */
145 if (ret == 0 &&
146 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
147 cachefiles_attr_changed(&object->fscache);
149 if (ret < 0 && ret != -ETIMEDOUT) {
150 if (ret != -ENOBUFS)
151 printk(KERN_WARNING
152 "CacheFiles: Lookup failed error %d\n", ret);
153 fscache_object_lookup_error(&object->fscache);
156 _leave(" [%d]", ret);
157 return ret;
161 * indication of lookup completion
163 static void cachefiles_lookup_complete(struct fscache_object *_object)
165 struct cachefiles_object *object;
167 object = container_of(_object, struct cachefiles_object, fscache);
169 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
171 if (object->lookup_data) {
172 kfree(object->lookup_data->key);
173 kfree(object->lookup_data->auxdata);
174 kfree(object->lookup_data);
175 object->lookup_data = NULL;
180 * increment the usage count on an inode object (may fail if unmounting)
182 static
183 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
185 struct cachefiles_object *object =
186 container_of(_object, struct cachefiles_object, fscache);
188 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
190 #ifdef CACHEFILES_DEBUG_SLAB
191 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
192 #endif
194 atomic_inc(&object->usage);
195 return &object->fscache;
199 * update the auxiliary data for an object object on disk
201 static void cachefiles_update_object(struct fscache_object *_object)
203 struct cachefiles_object *object;
204 struct cachefiles_xattr *auxdata;
205 struct cachefiles_cache *cache;
206 struct fscache_cookie *cookie;
207 const struct cred *saved_cred;
208 unsigned auxlen;
210 _enter("{OBJ%x}", _object->debug_id);
212 object = container_of(_object, struct cachefiles_object, fscache);
213 cache = container_of(object->fscache.cache, struct cachefiles_cache,
214 cache);
215 cookie = object->fscache.cookie;
217 if (!cookie->def->get_aux) {
218 _leave(" [no aux]");
219 return;
222 auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
223 if (!auxdata) {
224 _leave(" [nomem]");
225 return;
228 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
229 ASSERTCMP(auxlen, <, 511);
231 auxdata->len = auxlen + 1;
232 auxdata->type = cookie->def->type;
234 cachefiles_begin_secure(cache, &saved_cred);
235 cachefiles_update_object_xattr(object, auxdata);
236 cachefiles_end_secure(cache, saved_cred);
237 kfree(auxdata);
238 _leave("");
242 * discard the resources pinned by an object and effect retirement if
243 * requested
245 static void cachefiles_drop_object(struct fscache_object *_object)
247 struct cachefiles_object *object;
248 struct cachefiles_cache *cache;
249 const struct cred *saved_cred;
251 ASSERT(_object);
253 object = container_of(_object, struct cachefiles_object, fscache);
255 _enter("{OBJ%x,%d}",
256 object->fscache.debug_id, atomic_read(&object->usage));
258 cache = container_of(object->fscache.cache,
259 struct cachefiles_cache, cache);
261 #ifdef CACHEFILES_DEBUG_SLAB
262 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
263 #endif
265 /* delete retired objects */
266 if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
267 _object != cache->cache.fsdef
269 _debug("- retire object OBJ%x", object->fscache.debug_id);
270 cachefiles_begin_secure(cache, &saved_cred);
271 cachefiles_delete_object(cache, object);
272 cachefiles_end_secure(cache, saved_cred);
275 /* close the filesystem stuff attached to the object */
276 if (object->backer != object->dentry)
277 dput(object->backer);
278 object->backer = NULL;
280 /* note that the object is now inactive */
281 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
282 write_lock(&cache->active_lock);
283 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
284 &object->flags))
285 BUG();
286 rb_erase(&object->active_node, &cache->active_nodes);
287 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
288 write_unlock(&cache->active_lock);
291 dput(object->dentry);
292 object->dentry = NULL;
294 _leave("");
298 * dispose of a reference to an object
300 static void cachefiles_put_object(struct fscache_object *_object)
302 struct cachefiles_object *object;
303 struct fscache_cache *cache;
305 ASSERT(_object);
307 object = container_of(_object, struct cachefiles_object, fscache);
309 _enter("{OBJ%x,%d}",
310 object->fscache.debug_id, atomic_read(&object->usage));
312 #ifdef CACHEFILES_DEBUG_SLAB
313 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
314 #endif
316 ASSERTIFCMP(object->fscache.parent,
317 object->fscache.parent->n_children, >, 0);
319 if (atomic_dec_and_test(&object->usage)) {
320 _debug("- kill object OBJ%x", object->fscache.debug_id);
322 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
323 ASSERTCMP(object->fscache.parent, ==, NULL);
324 ASSERTCMP(object->backer, ==, NULL);
325 ASSERTCMP(object->dentry, ==, NULL);
326 ASSERTCMP(object->fscache.n_ops, ==, 0);
327 ASSERTCMP(object->fscache.n_children, ==, 0);
329 if (object->lookup_data) {
330 kfree(object->lookup_data->key);
331 kfree(object->lookup_data->auxdata);
332 kfree(object->lookup_data);
333 object->lookup_data = NULL;
336 cache = object->fscache.cache;
337 fscache_object_destroy(&object->fscache);
338 kmem_cache_free(cachefiles_object_jar, object);
339 fscache_object_destroyed(cache);
342 _leave("");
346 * sync a cache
348 static void cachefiles_sync_cache(struct fscache_cache *_cache)
350 struct cachefiles_cache *cache;
351 const struct cred *saved_cred;
352 int ret;
354 _enter("%p", _cache);
356 cache = container_of(_cache, struct cachefiles_cache, cache);
358 /* make sure all pages pinned by operations on behalf of the netfs are
359 * written to disc */
360 cachefiles_begin_secure(cache, &saved_cred);
361 down_read(&cache->mnt->mnt_sb->s_umount);
362 ret = sync_filesystem(cache->mnt->mnt_sb);
363 up_read(&cache->mnt->mnt_sb->s_umount);
364 cachefiles_end_secure(cache, saved_cred);
366 if (ret == -EIO)
367 cachefiles_io_error(cache,
368 "Attempt to sync backing fs superblock"
369 " returned error %d",
370 ret);
374 * notification the attributes on an object have changed
375 * - called with reads/writes excluded by FS-Cache
377 static int cachefiles_attr_changed(struct fscache_object *_object)
379 struct cachefiles_object *object;
380 struct cachefiles_cache *cache;
381 const struct cred *saved_cred;
382 struct iattr newattrs;
383 uint64_t ni_size;
384 loff_t oi_size;
385 int ret;
387 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
389 _enter("{OBJ%x},[%llu]",
390 _object->debug_id, (unsigned long long) ni_size);
392 object = container_of(_object, struct cachefiles_object, fscache);
393 cache = container_of(object->fscache.cache,
394 struct cachefiles_cache, cache);
396 if (ni_size == object->i_size)
397 return 0;
399 if (!object->backer)
400 return -ENOBUFS;
402 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
404 fscache_set_store_limit(&object->fscache, ni_size);
406 oi_size = i_size_read(object->backer->d_inode);
407 if (oi_size == ni_size)
408 return 0;
410 cachefiles_begin_secure(cache, &saved_cred);
411 mutex_lock(&object->backer->d_inode->i_mutex);
413 /* if there's an extension to a partial page at the end of the backing
414 * file, we need to discard the partial page so that we pick up new
415 * data after it */
416 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
417 _debug("discard tail %llx", oi_size);
418 newattrs.ia_valid = ATTR_SIZE;
419 newattrs.ia_size = oi_size & PAGE_MASK;
420 ret = notify_change(object->backer, &newattrs);
421 if (ret < 0)
422 goto truncate_failed;
425 newattrs.ia_valid = ATTR_SIZE;
426 newattrs.ia_size = ni_size;
427 ret = notify_change(object->backer, &newattrs);
429 truncate_failed:
430 mutex_unlock(&object->backer->d_inode->i_mutex);
431 cachefiles_end_secure(cache, saved_cred);
433 if (ret == -EIO) {
434 fscache_set_store_limit(&object->fscache, 0);
435 cachefiles_io_error_obj(object, "Size set failed");
436 ret = -ENOBUFS;
439 _leave(" = %d", ret);
440 return ret;
444 * dissociate a cache from all the pages it was backing
446 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
448 _enter("");
451 const struct fscache_cache_ops cachefiles_cache_ops = {
452 .name = "cachefiles",
453 .alloc_object = cachefiles_alloc_object,
454 .lookup_object = cachefiles_lookup_object,
455 .lookup_complete = cachefiles_lookup_complete,
456 .grab_object = cachefiles_grab_object,
457 .update_object = cachefiles_update_object,
458 .drop_object = cachefiles_drop_object,
459 .put_object = cachefiles_put_object,
460 .sync_cache = cachefiles_sync_cache,
461 .attr_changed = cachefiles_attr_changed,
462 .read_or_alloc_page = cachefiles_read_or_alloc_page,
463 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
464 .allocate_page = cachefiles_allocate_page,
465 .allocate_pages = cachefiles_allocate_pages,
466 .write_page = cachefiles_write_page,
467 .uncache_page = cachefiles_uncache_page,
468 .dissociate_pages = cachefiles_dissociate_pages,