blob: 232426214fdd1849b21a4e3c080563e2132e8bd2 [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
David Howells9ae326a2009-04-03 16:42:41 +010016struct cachefiles_lookup_data {
17 struct cachefiles_xattr *auxdata; /* auxiliary data */
18 char *key; /* key path */
19};
20
21static int cachefiles_attr_changed(struct fscache_object *_object);
22
23/*
24 * allocate an object record for a cookie lookup and prepare the lookup data
25 */
26static struct fscache_object *cachefiles_alloc_object(
27 struct fscache_cache *_cache,
28 struct fscache_cookie *cookie)
29{
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;
35 void *buffer;
36 char *key;
37
38 cache = container_of(_cache, struct cachefiles_cache, cache);
39
40 _enter("{%s},%p,", cache->cache.identifier, cookie);
41
David Howells5f4f9f42012-12-20 21:52:33 +000042 lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010043 if (!lookup_data)
44 goto nomem_lookup_data;
45
46 /* create a new object record and a temporary leaf image */
David Howells5f4f9f42012-12-20 21:52:33 +000047 object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010048 if (!object)
49 goto nomem_object;
50
51 ASSERTCMP(object->backer, ==, NULL);
52
53 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
54 atomic_set(&object->usage, 1);
55
56 fscache_object_init(&object->fscache, cookie, &cache->cache);
57
58 object->type = cookie->def->type;
59
60 /* get hold of the raw key
61 * - stick the length on the front and leave space on the back for the
62 * encoder
63 */
David Howells5f4f9f42012-12-20 21:52:33 +000064 buffer = kmalloc((2 + 512) + 3, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +010065 if (!buffer)
66 goto nomem_buffer;
67
68 keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512);
69 ASSERTCMP(keylen, <, 512);
70
71 *(uint16_t *)buffer = keylen;
72 ((char *)buffer)[keylen + 2] = 0;
73 ((char *)buffer)[keylen + 3] = 0;
74 ((char *)buffer)[keylen + 4] = 0;
75
76 /* turn the raw key into something that can work with as a filename */
77 key = cachefiles_cook_key(buffer, keylen + 2, object->type);
78 if (!key)
79 goto nomem_key;
80
81 /* get hold of the auxiliary data and prepend the object type */
82 auxdata = buffer;
83 auxlen = 0;
84 if (cookie->def->get_aux) {
85 auxlen = cookie->def->get_aux(cookie->netfs_data,
86 auxdata->data, 511);
87 ASSERTCMP(auxlen, <, 511);
88 }
89
90 auxdata->len = auxlen + 1;
91 auxdata->type = cookie->def->type;
92
93 lookup_data->auxdata = auxdata;
94 lookup_data->key = key;
95 object->lookup_data = lookup_data;
96
97 _leave(" = %p [%p]", &object->fscache, lookup_data);
98 return &object->fscache;
99
100nomem_key:
101 kfree(buffer);
102nomem_buffer:
103 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
104 kmem_cache_free(cachefiles_object_jar, object);
105 fscache_object_destroyed(&cache->cache);
106nomem_object:
107 kfree(lookup_data);
108nomem_lookup_data:
109 _leave(" = -ENOMEM");
110 return ERR_PTR(-ENOMEM);
111}
112
113/*
114 * attempt to look up the nominated node in this cache
David Howellsfee096d2009-11-19 18:12:05 +0000115 * - return -ETIMEDOUT to be scheduled again
David Howells9ae326a2009-04-03 16:42:41 +0100116 */
David Howellsfee096d2009-11-19 18:12:05 +0000117static int cachefiles_lookup_object(struct fscache_object *_object)
David Howells9ae326a2009-04-03 16:42:41 +0100118{
119 struct cachefiles_lookup_data *lookup_data;
120 struct cachefiles_object *parent, *object;
121 struct cachefiles_cache *cache;
122 const struct cred *saved_cred;
123 int ret;
124
125 _enter("{OBJ%x}", _object->debug_id);
126
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;
132
133 ASSERTCMP(lookup_data, !=, NULL);
134
135 /* look up the key, creating any missing bits */
136 cachefiles_begin_secure(cache, &saved_cred);
137 ret = cachefiles_walk_to_object(parent, object,
138 lookup_data->key,
139 lookup_data->auxdata);
140 cachefiles_end_secure(cache, saved_cred);
141
142 /* polish off by setting the attributes of non-index files */
143 if (ret == 0 &&
144 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
145 cachefiles_attr_changed(&object->fscache);
146
David Howellsfee096d2009-11-19 18:12:05 +0000147 if (ret < 0 && ret != -ETIMEDOUT) {
David Howells14e69642009-11-19 18:12:08 +0000148 if (ret != -ENOBUFS)
Fabian Frederick0227d6ab2014-06-06 14:37:33 -0700149 pr_warn("Lookup failed error %d\n", ret);
David Howells9ae326a2009-04-03 16:42:41 +0100150 fscache_object_lookup_error(&object->fscache);
151 }
152
153 _leave(" [%d]", ret);
David Howellsfee096d2009-11-19 18:12:05 +0000154 return ret;
David Howells9ae326a2009-04-03 16:42:41 +0100155}
156
157/*
158 * indication of lookup completion
159 */
160static void cachefiles_lookup_complete(struct fscache_object *_object)
161{
162 struct cachefiles_object *object;
163
164 object = container_of(_object, struct cachefiles_object, fscache);
165
166 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
167
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;
173 }
174}
175
176/*
177 * increment the usage count on an inode object (may fail if unmounting)
178 */
179static
180struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
181{
182 struct cachefiles_object *object =
183 container_of(_object, struct cachefiles_object, fscache);
184
185 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
186
187#ifdef CACHEFILES_DEBUG_SLAB
188 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
189#endif
190
191 atomic_inc(&object->usage);
192 return &object->fscache;
193}
194
195/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300196 * update the auxiliary data for an object object on disk
David Howells9ae326a2009-04-03 16:42:41 +0100197 */
198static void cachefiles_update_object(struct fscache_object *_object)
199{
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;
205 unsigned auxlen;
206
207 _enter("{OBJ%x}", _object->debug_id);
208
209 object = container_of(_object, struct cachefiles_object, fscache);
210 cache = container_of(object->fscache.cache, struct cachefiles_cache,
211 cache);
David Howells13627292013-05-10 19:50:26 +0100212
213 if (!fscache_use_cookie(_object)) {
214 _leave(" [relinq]");
215 return;
216 }
217
David Howells9ae326a2009-04-03 16:42:41 +0100218 cookie = object->fscache.cookie;
219
220 if (!cookie->def->get_aux) {
David Howells13627292013-05-10 19:50:26 +0100221 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100222 _leave(" [no aux]");
223 return;
224 }
225
David Howells5f4f9f42012-12-20 21:52:33 +0000226 auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
David Howells9ae326a2009-04-03 16:42:41 +0100227 if (!auxdata) {
David Howells13627292013-05-10 19:50:26 +0100228 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100229 _leave(" [nomem]");
230 return;
231 }
232
233 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
David Howells13627292013-05-10 19:50:26 +0100234 fscache_unuse_cookie(_object);
David Howells9ae326a2009-04-03 16:42:41 +0100235 ASSERTCMP(auxlen, <, 511);
236
237 auxdata->len = auxlen + 1;
238 auxdata->type = cookie->def->type;
239
240 cachefiles_begin_secure(cache, &saved_cred);
241 cachefiles_update_object_xattr(object, auxdata);
242 cachefiles_end_secure(cache, saved_cred);
243 kfree(auxdata);
244 _leave("");
245}
246
247/*
248 * discard the resources pinned by an object and effect retirement if
249 * requested
250 */
251static void cachefiles_drop_object(struct fscache_object *_object)
252{
253 struct cachefiles_object *object;
254 struct cachefiles_cache *cache;
255 const struct cred *saved_cred;
256
257 ASSERT(_object);
258
259 object = container_of(_object, struct cachefiles_object, fscache);
260
261 _enter("{OBJ%x,%d}",
262 object->fscache.debug_id, atomic_read(&object->usage));
263
264 cache = container_of(object->fscache.cache,
265 struct cachefiles_cache, cache);
266
267#ifdef CACHEFILES_DEBUG_SLAB
268 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
269#endif
270
David Howellsa3b7c002014-09-30 14:50:28 +0100271 /* We need to tidy the object up if we did in fact manage to open it.
272 * It's possible for us to get here before the object is fully
273 * initialised if the parent goes away or the object gets retired
274 * before we set it up.
275 */
276 if (object->dentry) {
277 /* delete retired objects */
278 if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
279 _object != cache->cache.fsdef
280 ) {
281 _debug("- retire object OBJ%x", object->fscache.debug_id);
282 cachefiles_begin_secure(cache, &saved_cred);
283 cachefiles_delete_object(cache, object);
284 cachefiles_end_secure(cache, saved_cred);
285 }
David Howells9ae326a2009-04-03 16:42:41 +0100286
David Howellsa3b7c002014-09-30 14:50:28 +0100287 /* close the filesystem stuff attached to the object */
288 if (object->backer != object->dentry)
289 dput(object->backer);
290 object->backer = NULL;
291 }
David Howells9ae326a2009-04-03 16:42:41 +0100292
293 /* note that the object is now inactive */
294 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
295 write_lock(&cache->active_lock);
296 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE,
297 &object->flags))
298 BUG();
299 rb_erase(&object->active_node, &cache->active_nodes);
300 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
301 write_unlock(&cache->active_lock);
302 }
303
304 dput(object->dentry);
305 object->dentry = NULL;
306
307 _leave("");
308}
309
310/*
311 * dispose of a reference to an object
312 */
313static void cachefiles_put_object(struct fscache_object *_object)
314{
315 struct cachefiles_object *object;
316 struct fscache_cache *cache;
317
318 ASSERT(_object);
319
320 object = container_of(_object, struct cachefiles_object, fscache);
321
322 _enter("{OBJ%x,%d}",
323 object->fscache.debug_id, atomic_read(&object->usage));
324
325#ifdef CACHEFILES_DEBUG_SLAB
326 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
327#endif
328
329 ASSERTIFCMP(object->fscache.parent,
330 object->fscache.parent->n_children, >, 0);
331
332 if (atomic_dec_and_test(&object->usage)) {
333 _debug("- kill object OBJ%x", object->fscache.debug_id);
334
335 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
336 ASSERTCMP(object->fscache.parent, ==, NULL);
337 ASSERTCMP(object->backer, ==, NULL);
338 ASSERTCMP(object->dentry, ==, NULL);
339 ASSERTCMP(object->fscache.n_ops, ==, 0);
340 ASSERTCMP(object->fscache.n_children, ==, 0);
341
342 if (object->lookup_data) {
343 kfree(object->lookup_data->key);
344 kfree(object->lookup_data->auxdata);
345 kfree(object->lookup_data);
346 object->lookup_data = NULL;
347 }
348
349 cache = object->fscache.cache;
David Howells4fbf4292009-11-19 18:11:04 +0000350 fscache_object_destroy(&object->fscache);
David Howells9ae326a2009-04-03 16:42:41 +0100351 kmem_cache_free(cachefiles_object_jar, object);
352 fscache_object_destroyed(cache);
353 }
354
355 _leave("");
356}
357
358/*
359 * sync a cache
360 */
361static void cachefiles_sync_cache(struct fscache_cache *_cache)
362{
363 struct cachefiles_cache *cache;
364 const struct cred *saved_cred;
365 int ret;
366
367 _enter("%p", _cache);
368
369 cache = container_of(_cache, struct cachefiles_cache, cache);
370
371 /* make sure all pages pinned by operations on behalf of the netfs are
372 * written to disc */
373 cachefiles_begin_secure(cache, &saved_cred);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200374 down_read(&cache->mnt->mnt_sb->s_umount);
Jan Kara60b06802009-04-27 16:43:53 +0200375 ret = sync_filesystem(cache->mnt->mnt_sb);
Christoph Hellwig5af79262009-05-05 15:41:25 +0200376 up_read(&cache->mnt->mnt_sb->s_umount);
David Howells9ae326a2009-04-03 16:42:41 +0100377 cachefiles_end_secure(cache, saved_cred);
378
379 if (ret == -EIO)
380 cachefiles_io_error(cache,
381 "Attempt to sync backing fs superblock"
382 " returned error %d",
383 ret);
384}
385
386/*
David Howells5002d7b2013-08-21 17:29:21 -0400387 * check if the backing cache is updated to FS-Cache
388 * - called by FS-Cache when evaluates if need to invalidate the cache
389 */
390static bool cachefiles_check_consistency(struct fscache_operation *op)
391{
392 struct cachefiles_object *object;
393 struct cachefiles_cache *cache;
394 const struct cred *saved_cred;
395 int ret;
396
397 _enter("{OBJ%x}", op->object->debug_id);
398
399 object = container_of(op->object, struct cachefiles_object, fscache);
400 cache = container_of(object->fscache.cache,
401 struct cachefiles_cache, cache);
402
403 cachefiles_begin_secure(cache, &saved_cred);
404 ret = cachefiles_check_auxdata(object);
405 cachefiles_end_secure(cache, saved_cred);
406
407 _leave(" = %d", ret);
408 return ret;
409}
410
411/*
David Howells9ae326a2009-04-03 16:42:41 +0100412 * notification the attributes on an object have changed
413 * - called with reads/writes excluded by FS-Cache
414 */
415static int cachefiles_attr_changed(struct fscache_object *_object)
416{
417 struct cachefiles_object *object;
418 struct cachefiles_cache *cache;
419 const struct cred *saved_cred;
420 struct iattr newattrs;
421 uint64_t ni_size;
422 loff_t oi_size;
423 int ret;
424
425 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
426
427 _enter("{OBJ%x},[%llu]",
428 _object->debug_id, (unsigned long long) ni_size);
429
430 object = container_of(_object, struct cachefiles_object, fscache);
431 cache = container_of(object->fscache.cache,
432 struct cachefiles_cache, cache);
433
434 if (ni_size == object->i_size)
435 return 0;
436
437 if (!object->backer)
438 return -ENOBUFS;
439
David Howellsce40fa72015-01-29 12:02:36 +0000440 ASSERT(d_is_reg(object->backer));
David Howells9ae326a2009-04-03 16:42:41 +0100441
442 fscache_set_store_limit(&object->fscache, ni_size);
443
444 oi_size = i_size_read(object->backer->d_inode);
445 if (oi_size == ni_size)
446 return 0;
447
David Howells9ae326a2009-04-03 16:42:41 +0100448 cachefiles_begin_secure(cache, &saved_cred);
449 mutex_lock(&object->backer->d_inode->i_mutex);
David Howellsa17754f2009-11-19 18:11:52 +0000450
451 /* if there's an extension to a partial page at the end of the backing
452 * file, we need to discard the partial page so that we pick up new
453 * data after it */
454 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
455 _debug("discard tail %llx", oi_size);
456 newattrs.ia_valid = ATTR_SIZE;
457 newattrs.ia_size = oi_size & PAGE_MASK;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400458 ret = notify_change(object->backer, &newattrs, NULL);
David Howellsa17754f2009-11-19 18:11:52 +0000459 if (ret < 0)
460 goto truncate_failed;
461 }
462
463 newattrs.ia_valid = ATTR_SIZE;
464 newattrs.ia_size = ni_size;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400465 ret = notify_change(object->backer, &newattrs, NULL);
David Howellsa17754f2009-11-19 18:11:52 +0000466
467truncate_failed:
David Howells9ae326a2009-04-03 16:42:41 +0100468 mutex_unlock(&object->backer->d_inode->i_mutex);
469 cachefiles_end_secure(cache, saved_cred);
470
471 if (ret == -EIO) {
472 fscache_set_store_limit(&object->fscache, 0);
473 cachefiles_io_error_obj(object, "Size set failed");
474 ret = -ENOBUFS;
475 }
476
477 _leave(" = %d", ret);
478 return ret;
479}
480
481/*
David Howells9dc8d9b2012-12-20 21:52:36 +0000482 * Invalidate an object
483 */
484static void cachefiles_invalidate_object(struct fscache_operation *op)
485{
486 struct cachefiles_object *object;
487 struct cachefiles_cache *cache;
488 const struct cred *saved_cred;
489 struct path path;
490 uint64_t ni_size;
491 int ret;
492
493 object = container_of(op->object, struct cachefiles_object, fscache);
494 cache = container_of(object->fscache.cache,
495 struct cachefiles_cache, cache);
496
497 op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
498 &ni_size);
499
500 _enter("{OBJ%x},[%llu]",
501 op->object->debug_id, (unsigned long long)ni_size);
502
503 if (object->backer) {
David Howellsce40fa72015-01-29 12:02:36 +0000504 ASSERT(d_is_reg(object->backer));
David Howells9dc8d9b2012-12-20 21:52:36 +0000505
506 fscache_set_store_limit(&object->fscache, ni_size);
507
508 path.dentry = object->backer;
509 path.mnt = cache->mnt;
510
511 cachefiles_begin_secure(cache, &saved_cred);
512 ret = vfs_truncate(&path, 0);
513 if (ret == 0)
514 ret = vfs_truncate(&path, ni_size);
515 cachefiles_end_secure(cache, saved_cred);
516
517 if (ret != 0) {
518 fscache_set_store_limit(&object->fscache, 0);
519 if (ret == -EIO)
520 cachefiles_io_error_obj(object,
521 "Invalidate failed");
522 }
523 }
524
David Howells1f372df2012-12-13 20:03:13 +0000525 fscache_op_complete(op, true);
David Howells9dc8d9b2012-12-20 21:52:36 +0000526 _leave("");
527}
528
529/*
David Howells9ae326a2009-04-03 16:42:41 +0100530 * dissociate a cache from all the pages it was backing
531 */
532static void cachefiles_dissociate_pages(struct fscache_cache *cache)
533{
534 _enter("");
535}
536
537const struct fscache_cache_ops cachefiles_cache_ops = {
538 .name = "cachefiles",
539 .alloc_object = cachefiles_alloc_object,
540 .lookup_object = cachefiles_lookup_object,
541 .lookup_complete = cachefiles_lookup_complete,
542 .grab_object = cachefiles_grab_object,
543 .update_object = cachefiles_update_object,
David Howells9dc8d9b2012-12-20 21:52:36 +0000544 .invalidate_object = cachefiles_invalidate_object,
David Howells9ae326a2009-04-03 16:42:41 +0100545 .drop_object = cachefiles_drop_object,
546 .put_object = cachefiles_put_object,
547 .sync_cache = cachefiles_sync_cache,
548 .attr_changed = cachefiles_attr_changed,
549 .read_or_alloc_page = cachefiles_read_or_alloc_page,
550 .read_or_alloc_pages = cachefiles_read_or_alloc_pages,
551 .allocate_page = cachefiles_allocate_page,
552 .allocate_pages = cachefiles_allocate_pages,
553 .write_page = cachefiles_write_page,
554 .uncache_page = cachefiles_uncache_page,
555 .dissociate_pages = cachefiles_dissociate_pages,
David Howells5002d7b2013-08-21 17:29:21 -0400556 .check_consistency = cachefiles_check_consistency,
David Howells9ae326a2009-04-03 16:42:41 +0100557};