blob: eeb3f7d0e1a78dd2912bbb3289ab244cc8fe576d [file] [log] [blame]
David Howells9ae326a2009-04-03 16:42:41 +01001/* FS-Cache interface to CacheFiles
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
David Howells9ae326a2009-04-03 16:42:41 +010013#include <linux/mount.h>
David Howells9ae326a2009-04-03 16:42:41 +010014#include "internal.h"
15
16#define list_to_page(head) (list_entry((head)->prev, struct page, lru))
17
18struct cachefiles_lookup_data {
19 struct cachefiles_xattr *auxdata; /* auxiliary data */
20 char *key; /* key path */
21};
22
23static int cachefiles_attr_changed(struct fscache_object *_object);
24
25/*
26 * allocate an object record for a cookie lookup and prepare the lookup data
27 */
28static struct fscache_object *cachefiles_alloc_object(
29 struct fscache_cache *_cache,
30 struct fscache_cookie *cookie)
31{
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;
39
40 cache = container_of(_cache, struct cachefiles_cache, cache);
41
42 _enter("{%s},%p,", cache->cache.identifier, cookie);
43
David Howells5f4f9f42012-12-20 21:52:33 +000044 lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010045 if (!lookup_data)
46 goto nomem_lookup_data;
47
48 /* create a new object record and a temporary leaf image */
David Howells5f4f9f42012-12-20 21:52:33 +000049 object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010050 if (!object)
51 goto nomem_object;
52
53 ASSERTCMP(object->backer, ==, NULL);
54
55 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
56 atomic_set(&object->usage, 1);
57
58 fscache_object_init(&object->fscache, cookie, &cache->cache);
59
60 object->type = cookie->def->type;
61
62 /* get hold of the raw key
63 * - stick the length on the front and leave space on the back for the
64 * encoder
65 */
David Howells5f4f9f42012-12-20 21:52:33 +000066 buffer = kmalloc((2 + 512) + 3, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010067 if (!buffer)
68 goto nomem_buffer;
69
70 keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512);
71 ASSERTCMP(keylen, <, 512);
72
73 *(uint16_t *)buffer = keylen;
74 ((char *)buffer)[keylen + 2] = 0;
75 ((char *)buffer)[keylen + 3] = 0;
76 ((char *)buffer)[keylen + 4] = 0;
77
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;
82
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);
90 }
91
92 auxdata->len = auxlen + 1;
93 auxdata->type = cookie->def->type;
94
95 lookup_data->auxdata = auxdata;
96 lookup_data->key = key;
97 object->lookup_data = lookup_data;
98
99 _leave(" = %p [%p]", &object->fscache, lookup_data);
100 return &object->fscache;
101
102nomem_key:
103 kfree(buffer);
104nomem_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);
108nomem_object:
109 kfree(lookup_data);
110nomem_lookup_data:
111 _leave(" = -ENOMEM");
112 return ERR_PTR(-ENOMEM);
113}
114
115/*
116 * attempt to look up the nominated node in this cache
David Howellsfee096d2009-11-19 18:12:05 +0000117 * - return -ETIMEDOUT to be scheduled again
David Howells9ae326a2009-04-03 16:42:41 +0100118 */
David Howellsfee096d2009-11-19 18:12:05 +0000119static int cachefiles_lookup_object(struct fscache_object *_object)
David Howells9ae326a2009-04-03 16:42:41 +0100120{
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;
126
127 _enter("{OBJ%x}", _object->debug_id);
128
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;
134
135 ASSERTCMP(lookup_data, !=, NULL);
136
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);
143
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);
148
David Howellsfee096d2009-11-19 18:12:05 +0000149 if (ret < 0 && ret != -ETIMEDOUT) {
David Howells14e69642009-11-19 18:12:08 +0000150 if (ret != -ENOBUFS)
151 printk(KERN_WARNING
152 "CacheFiles: Lookup failed error %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100153 fscache_object_lookup_error(&object->fscache);
154 }
155
156 _leave(" [%d]", ret);
David Howellsfee096d2009-11-19 18:12:05 +0000157 return ret;
David Howells9ae326a2009-04-03 16:42:41 +0100158}
159
160/*
161 * indication of lookup completion
162 */
163static void cachefiles_lookup_complete(struct fscache_object *_object)
164{
165 struct cachefiles_object *object;
166
167 object = container_of(_object, struct cachefiles_object, fscache);
168
169 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
170
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;
176 }
177}
178
179/*
180 * increment the usage count on an inode object (may fail if unmounting)
181 */
182static
183struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
184{
185 struct cachefiles_object *object =
186 container_of(_object, struct cachefiles_object, fscache);
187
188 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
189
190#ifdef CACHEFILES_DEBUG_SLAB
191 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
192#endif
193
194 atomic_inc(&object->usage);
195 return &object->fscache;
196}
197
198/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300199 * update the auxiliary data for an object object on disk
David Howells9ae326a2009-04-03 16:42:41 +0100200 */
201static void cachefiles_update_object(struct fscache_object *_object)
202{
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;
209
210 _enter("{OBJ%x}", _object->debug_id);
211
212 object = container_of(_object, struct cachefiles_object, fscache);
213 cache = container_of(object->fscache.cache, struct cachefiles_cache,
214 cache);
David Howells13627292013-05-10 19:50:26 +0100215
216 if (!fscache_use_cookie(_object)) {
217 _leave(" [relinq]");
218 return;
219 }
220
David Howells9ae326a2009-04-03 16:42:41 +0100221 cookie = object->fscache.cookie;
222
223 if (!cookie->def->get_aux) {
David Howells13627292013-05-10 19:50:26 +0100224 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100225 _leave(" [no aux]");
226 return;
227 }
228
David Howells5f4f9f42012-12-20 21:52:33 +0000229 auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100230 if (!auxdata) {
David Howells13627292013-05-10 19:50:26 +0100231 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100232 _leave(" [nomem]");
233 return;
234 }
235
236 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
David Howells13627292013-05-10 19:50:26 +0100237 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100238 ASSERTCMP(auxlen, <, 511);
239
240 auxdata->len = auxlen + 1;
241 auxdata->type = cookie->def->type;
242
243 cachefiles_begin_secure(cache, &saved_cred);
244 cachefiles_update_object_xattr(object, auxdata);
245 cachefiles_end_secure(cache, saved_cred);
246 kfree(auxdata);
247 _leave("");
248}
249
250/*
251 * discard the resources pinned by an object and effect retirement if
252 * requested
253 */
254static void cachefiles_drop_object(struct fscache_object *_object)
255{
256 struct cachefiles_object *object;
257 struct cachefiles_cache *cache;
258 const struct cred *saved_cred;
259
260 ASSERT(_object);
261
262 object = container_of(_object, struct cachefiles_object, fscache);
263
264 _enter("{OBJ%x,%d}",
265 object->fscache.debug_id, atomic_read(&object->usage));
266
267 cache = container_of(object->fscache.cache,
268 struct cachefiles_cache, cache);
269
270#ifdef CACHEFILES_DEBUG_SLAB
271 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
272#endif
273
274 /* delete retired objects */
David Howells13627292013-05-10 19:50:26 +0100275 if (test_bit(FSCACHE_COOKIE_RETIRED, &object->fscache.cookie->flags) &&
David Howells9ae326a2009-04-03 16:42:41 +0100276 _object != cache->cache.fsdef
277 ) {
278 _debug("- retire object OBJ%x", object->fscache.debug_id);
279 cachefiles_begin_secure(cache, &saved_cred);
280 cachefiles_delete_object(cache, object);
281 cachefiles_end_secure(cache, saved_cred);
282 }
283
284 /* close the filesystem stuff attached to the object */
285 if (object->backer != object->dentry)
286 dput(object->backer);
287 object->backer = NULL;
288
289 /* note that the object is now inactive */
290 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
291 write_lock(&cache->active_lock);
292 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
293 &object->flags))
294 BUG();
295 rb_erase(&object->active_node, &cache->active_nodes);
296 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
297 write_unlock(&cache->active_lock);
298 }
299
300 dput(object->dentry);
301 object->dentry = NULL;
302
303 _leave("");
304}
305
306/*
307 * dispose of a reference to an object
308 */
309static void cachefiles_put_object(struct fscache_object *_object)
310{
311 struct cachefiles_object *object;
312 struct fscache_cache *cache;
313
314 ASSERT(_object);
315
316 object = container_of(_object, struct cachefiles_object, fscache);
317
318 _enter("{OBJ%x,%d}",
319 object->fscache.debug_id, atomic_read(&object->usage));
320
321#ifdef CACHEFILES_DEBUG_SLAB
322 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
323#endif
324
325 ASSERTIFCMP(object->fscache.parent,
326 object->fscache.parent->n_children, >, 0);
327
328 if (atomic_dec_and_test(&object->usage)) {
329 _debug("- kill object OBJ%x", object->fscache.debug_id);
330
331 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
332 ASSERTCMP(object->fscache.parent, ==, NULL);
333 ASSERTCMP(object->backer, ==, NULL);
334 ASSERTCMP(object->dentry, ==, NULL);
335 ASSERTCMP(object->fscache.n_ops, ==, 0);
336 ASSERTCMP(object->fscache.n_children, ==, 0);
337
338 if (object->lookup_data) {
339 kfree(object->lookup_data->key);
340 kfree(object->lookup_data->auxdata);
341 kfree(object->lookup_data);
342 object->lookup_data = NULL;
343 }
344
345 cache = object->fscache.cache;
David Howells4fbf4292009-11-19 18:11:04 +0000346 fscache_object_destroy(&object->fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100347 kmem_cache_free(cachefiles_object_jar, object);
348 fscache_object_destroyed(cache);
349 }
350
351 _leave("");
352}
353
354/*
355 * sync a cache
356 */
357static void cachefiles_sync_cache(struct fscache_cache *_cache)
358{
359 struct cachefiles_cache *cache;
360 const struct cred *saved_cred;
361 int ret;
362
363 _enter("%p", _cache);
364
365 cache = container_of(_cache, struct cachefiles_cache, cache);
366
367 /* make sure all pages pinned by operations on behalf of the netfs are
368 * written to disc */
369 cachefiles_begin_secure(cache, &saved_cred);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200370 down_read(&cache->mnt->mnt_sb->s_umount);
Jan Kara60b06802009-04-27 16:43:53 +0200371 ret = sync_filesystem(cache->mnt->mnt_sb);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200372 up_read(&cache->mnt->mnt_sb->s_umount);
David Howells9ae326a2009-04-03 16:42:41 +0100373 cachefiles_end_secure(cache, saved_cred);
374
375 if (ret == -EIO)
376 cachefiles_io_error(cache,
377 "Attempt to sync backing fs superblock"
378 " returned error %d",
379 ret);
380}
381
382/*
383 * notification the attributes on an object have changed
384 * - called with reads/writes excluded by FS-Cache
385 */
386static int cachefiles_attr_changed(struct fscache_object *_object)
387{
388 struct cachefiles_object *object;
389 struct cachefiles_cache *cache;
390 const struct cred *saved_cred;
391 struct iattr newattrs;
392 uint64_t ni_size;
393 loff_t oi_size;
394 int ret;
395
396 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
397
398 _enter("{OBJ%x},[%llu]",
399 _object->debug_id, (unsigned long long) ni_size);
400
401 object = container_of(_object, struct cachefiles_object, fscache);
402 cache = container_of(object->fscache.cache,
403 struct cachefiles_cache, cache);
404
405 if (ni_size == object->i_size)
406 return 0;
407
408 if (!object->backer)
409 return -ENOBUFS;
410
411 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
412
413 fscache_set_store_limit(&object->fscache, ni_size);
414
415 oi_size = i_size_read(object->backer->d_inode);
416 if (oi_size == ni_size)
417 return 0;
418
David Howells9ae326a2009-04-03 16:42:41 +0100419 cachefiles_begin_secure(cache, &saved_cred);
420 mutex_lock(&object->backer->d_inode->i_mutex);
David Howellsa17754f2009-11-19 18:11:52 +0000421
422 /* if there's an extension to a partial page at the end of the backing
423 * file, we need to discard the partial page so that we pick up new
424 * data after it */
425 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
426 _debug("discard tail %llx", oi_size);
427 newattrs.ia_valid = ATTR_SIZE;
428 newattrs.ia_size = oi_size & PAGE_MASK;
429 ret = notify_change(object->backer, &newattrs);
430 if (ret < 0)
431 goto truncate_failed;
432 }
433
434 newattrs.ia_valid = ATTR_SIZE;
435 newattrs.ia_size = ni_size;
David Howells9ae326a2009-04-03 16:42:41 +0100436 ret = notify_change(object->backer, &newattrs);
David Howellsa17754f2009-11-19 18:11:52 +0000437
438truncate_failed:
David Howells9ae326a2009-04-03 16:42:41 +0100439 mutex_unlock(&object->backer->d_inode->i_mutex);
440 cachefiles_end_secure(cache, saved_cred);
441
442 if (ret == -EIO) {
443 fscache_set_store_limit(&object->fscache, 0);
444 cachefiles_io_error_obj(object, "Size set failed");
445 ret = -ENOBUFS;
446 }
447
448 _leave(" = %d", ret);
449 return ret;
450}
451
452/*
David Howells9dc8d9b2012-12-20 21:52:36 +0000453 * Invalidate an object
454 */
455static void cachefiles_invalidate_object(struct fscache_operation *op)
456{
457 struct cachefiles_object *object;
458 struct cachefiles_cache *cache;
459 const struct cred *saved_cred;
460 struct path path;
461 uint64_t ni_size;
462 int ret;
463
464 object = container_of(op->object, struct cachefiles_object, fscache);
465 cache = container_of(object->fscache.cache,
466 struct cachefiles_cache, cache);
467
468 op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
469 &ni_size);
470
471 _enter("{OBJ%x},[%llu]",
472 op->object->debug_id, (unsigned long long)ni_size);
473
474 if (object->backer) {
475 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
476
477 fscache_set_store_limit(&object->fscache, ni_size);
478
479 path.dentry = object->backer;
480 path.mnt = cache->mnt;
481
482 cachefiles_begin_secure(cache, &saved_cred);
483 ret = vfs_truncate(&path, 0);
484 if (ret == 0)
485 ret = vfs_truncate(&path, ni_size);
486 cachefiles_end_secure(cache, saved_cred);
487
488 if (ret != 0) {
489 fscache_set_store_limit(&object->fscache, 0);
490 if (ret == -EIO)
491 cachefiles_io_error_obj(object,
492 "Invalidate failed");
493 }
494 }
495
David Howells1f372df2012-12-13 20:03:13 +0000496 fscache_op_complete(op, true);
David Howells9dc8d9b2012-12-20 21:52:36 +0000497 _leave("");
498}
499
500/*
David Howells9ae326a2009-04-03 16:42:41 +0100501 * dissociate a cache from all the pages it was backing
502 */
503static void cachefiles_dissociate_pages(struct fscache_cache *cache)
504{
505 _enter("");
506}
507
508const struct fscache_cache_ops cachefiles_cache_ops = {
509 .name = "cachefiles",
510 .alloc_object = cachefiles_alloc_object,
511 .lookup_object = cachefiles_lookup_object,
512 .lookup_complete = cachefiles_lookup_complete,
513 .grab_object = cachefiles_grab_object,
514 .update_object = cachefiles_update_object,
David Howells9dc8d9b2012-12-20 21:52:36 +0000515 .invalidate_object = cachefiles_invalidate_object,
David Howells9ae326a2009-04-03 16:42:41 +0100516 .drop_object = cachefiles_drop_object,
517 .put_object = cachefiles_put_object,
518 .sync_cache = cachefiles_sync_cache,
519 .attr_changed = cachefiles_attr_changed,
520 .read_or_alloc_page = cachefiles_read_or_alloc_page,
521 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
522 .allocate_page = cachefiles_allocate_page,
523 .allocate_pages = cachefiles_allocate_pages,
524 .write_page = cachefiles_write_page,
525 .uncache_page = cachefiles_uncache_page,
526 .dissociate_pages = cachefiles_dissociate_pages,
527};