]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/cachefiles/interface.c
CacheFiles: Catch an overly long wait for an old active object
[net-next-2.6.git] / fs / cachefiles / interface.c
1 /* 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
12 #include <linux/mount.h>
13 #include <linux/buffer_head.h>
14 #include "internal.h"
15
16 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
17
18 struct cachefiles_lookup_data {
19         struct cachefiles_xattr *auxdata;       /* auxiliary data */
20         char                    *key;           /* key path */
21 };
22
23 static 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  */
28 static 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
44         lookup_data = kmalloc(sizeof(*lookup_data), GFP_KERNEL);
45         if (!lookup_data)
46                 goto nomem_lookup_data;
47
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;
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          */
66         buffer = kmalloc((2 + 512) + 3, GFP_KERNEL);
67         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
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);
113 }
114
115 /*
116  * attempt to look up the nominated node in this cache
117  * - return -ETIMEDOUT to be scheduled again
118  */
119 static int cachefiles_lookup_object(struct fscache_object *_object)
120 {
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
149         if (ret < 0 && ret != -ETIMEDOUT) {
150                 printk(KERN_WARNING "CacheFiles: Lookup failed error %d\n",
151                        ret);
152                 fscache_object_lookup_error(&object->fscache);
153         }
154
155         _leave(" [%d]", ret);
156         return ret;
157 }
158
159 /*
160  * indication of lookup completion
161  */
162 static void cachefiles_lookup_complete(struct fscache_object *_object)
163 {
164         struct cachefiles_object *object;
165
166         object = container_of(_object, struct cachefiles_object, fscache);
167
168         _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
169
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;
175         }
176 }
177
178 /*
179  * increment the usage count on an inode object (may fail if unmounting)
180  */
181 static
182 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
183 {
184         struct cachefiles_object *object =
185                 container_of(_object, struct cachefiles_object, fscache);
186
187         _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
188
189 #ifdef CACHEFILES_DEBUG_SLAB
190         ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
191 #endif
192
193         atomic_inc(&object->usage);
194         return &object->fscache;
195 }
196
197 /*
198  * update the auxilliary data for an object object on disk
199  */
200 static void cachefiles_update_object(struct fscache_object *_object)
201 {
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;
207         unsigned auxlen;
208
209         _enter("{OBJ%x}", _object->debug_id);
210
211         object = container_of(_object, struct cachefiles_object, fscache);
212         cache = container_of(object->fscache.cache, struct cachefiles_cache,
213                              cache);
214         cookie = object->fscache.cookie;
215
216         if (!cookie->def->get_aux) {
217                 _leave(" [no aux]");
218                 return;
219         }
220
221         auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
222         if (!auxdata) {
223                 _leave(" [nomem]");
224                 return;
225         }
226
227         auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
228         ASSERTCMP(auxlen, <, 511);
229
230         auxdata->len = auxlen + 1;
231         auxdata->type = cookie->def->type;
232
233         cachefiles_begin_secure(cache, &saved_cred);
234         cachefiles_update_object_xattr(object, auxdata);
235         cachefiles_end_secure(cache, saved_cred);
236         kfree(auxdata);
237         _leave("");
238 }
239
240 /*
241  * discard the resources pinned by an object and effect retirement if
242  * requested
243  */
244 static void cachefiles_drop_object(struct fscache_object *_object)
245 {
246         struct cachefiles_object *object;
247         struct cachefiles_cache *cache;
248         const struct cred *saved_cred;
249
250         ASSERT(_object);
251
252         object = container_of(_object, struct cachefiles_object, fscache);
253
254         _enter("{OBJ%x,%d}",
255                object->fscache.debug_id, atomic_read(&object->usage));
256
257         cache = container_of(object->fscache.cache,
258                              struct cachefiles_cache, cache);
259
260 #ifdef CACHEFILES_DEBUG_SLAB
261         ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
262 #endif
263
264         /* delete retired objects */
265         if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
266             _object != cache->cache.fsdef
267             ) {
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);
272         }
273
274         /* close the filesystem stuff attached to the object */
275         if (object->backer != object->dentry)
276                 dput(object->backer);
277         object->backer = NULL;
278
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,
283                                         &object->flags))
284                         BUG();
285                 rb_erase(&object->active_node, &cache->active_nodes);
286                 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
287                 write_unlock(&cache->active_lock);
288         }
289
290         dput(object->dentry);
291         object->dentry = NULL;
292
293         _leave("");
294 }
295
296 /*
297  * dispose of a reference to an object
298  */
299 static void cachefiles_put_object(struct fscache_object *_object)
300 {
301         struct cachefiles_object *object;
302         struct fscache_cache *cache;
303
304         ASSERT(_object);
305
306         object = container_of(_object, struct cachefiles_object, fscache);
307
308         _enter("{OBJ%x,%d}",
309                object->fscache.debug_id, atomic_read(&object->usage));
310
311 #ifdef CACHEFILES_DEBUG_SLAB
312         ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
313 #endif
314
315         ASSERTIFCMP(object->fscache.parent,
316                     object->fscache.parent->n_children, >, 0);
317
318         if (atomic_dec_and_test(&object->usage)) {
319                 _debug("- kill object OBJ%x", object->fscache.debug_id);
320
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);
327
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;
333                 }
334
335                 cache = object->fscache.cache;
336                 fscache_object_destroy(&object->fscache);
337                 kmem_cache_free(cachefiles_object_jar, object);
338                 fscache_object_destroyed(cache);
339         }
340
341         _leave("");
342 }
343
344 /*
345  * sync a cache
346  */
347 static void cachefiles_sync_cache(struct fscache_cache *_cache)
348 {
349         struct cachefiles_cache *cache;
350         const struct cred *saved_cred;
351         int ret;
352
353         _enter("%p", _cache);
354
355         cache = container_of(_cache, struct cachefiles_cache, cache);
356
357         /* make sure all pages pinned by operations on behalf of the netfs are
358          * written to disc */
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);
364
365         if (ret == -EIO)
366                 cachefiles_io_error(cache,
367                                     "Attempt to sync backing fs superblock"
368                                     " returned error %d",
369                                     ret);
370 }
371
372 /*
373  * notification the attributes on an object have changed
374  * - called with reads/writes excluded by FS-Cache
375  */
376 static int cachefiles_attr_changed(struct fscache_object *_object)
377 {
378         struct cachefiles_object *object;
379         struct cachefiles_cache *cache;
380         const struct cred *saved_cred;
381         struct iattr newattrs;
382         uint64_t ni_size;
383         loff_t oi_size;
384         int ret;
385
386         _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
387
388         _enter("{OBJ%x},[%llu]",
389                _object->debug_id, (unsigned long long) ni_size);
390
391         object = container_of(_object, struct cachefiles_object, fscache);
392         cache = container_of(object->fscache.cache,
393                              struct cachefiles_cache, cache);
394
395         if (ni_size == object->i_size)
396                 return 0;
397
398         if (!object->backer)
399                 return -ENOBUFS;
400
401         ASSERT(S_ISREG(object->backer->d_inode->i_mode));
402
403         fscache_set_store_limit(&object->fscache, ni_size);
404
405         oi_size = i_size_read(object->backer->d_inode);
406         if (oi_size == ni_size)
407                 return 0;
408
409         cachefiles_begin_secure(cache, &saved_cred);
410         mutex_lock(&object->backer->d_inode->i_mutex);
411
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
414          * data after it */
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);
420                 if (ret < 0)
421                         goto truncate_failed;
422         }
423
424         newattrs.ia_valid = ATTR_SIZE;
425         newattrs.ia_size = ni_size;
426         ret = notify_change(object->backer, &newattrs);
427
428 truncate_failed:
429         mutex_unlock(&object->backer->d_inode->i_mutex);
430         cachefiles_end_secure(cache, saved_cred);
431
432         if (ret == -EIO) {
433                 fscache_set_store_limit(&object->fscache, 0);
434                 cachefiles_io_error_obj(object, "Size set failed");
435                 ret = -ENOBUFS;
436         }
437
438         _leave(" = %d", ret);
439         return ret;
440 }
441
442 /*
443  * dissociate a cache from all the pages it was backing
444  */
445 static void cachefiles_dissociate_pages(struct fscache_cache *cache)
446 {
447         _enter("");
448 }
449
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,
468 };