Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* FS-Cache interface to CacheFiles
3 *
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/slab.h>
9#include <linux/mount.h>
10#include <linux/xattr.h>
11#include <linux/file.h>
12#include <linux/falloc.h>
13#include <trace/events/fscache.h>
14#include "internal.h"
15
16static atomic_t cachefiles_object_debug_id;
17
18/*
19 * Allocate a cache object record.
20 */
21static
22struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie)
23{
24 struct fscache_volume *vcookie = cookie->volume;
25 struct cachefiles_volume *volume = vcookie->cache_priv;
26 struct cachefiles_object *object;
27
28 _enter("{%s},%x,", vcookie->key, cookie->debug_id);
29
30 object = kmem_cache_zalloc(cachefiles_object_jar, GFP_KERNEL);
31 if (!object)
32 return NULL;
33
34 if (cachefiles_ondemand_init_obj_info(object, volume)) {
35 kmem_cache_free(cachefiles_object_jar, object);
36 return NULL;
37 }
38
39 refcount_set(&object->ref, 1);
40
41 spin_lock_init(&object->lock);
42 INIT_LIST_HEAD(&object->cache_link);
43 object->volume = volume;
44 object->debug_id = atomic_inc_return(&cachefiles_object_debug_id);
45 object->cookie = fscache_get_cookie(cookie, fscache_cookie_get_attach_object);
46
47 fscache_count_object(vcookie->cache);
48 trace_cachefiles_ref(object->debug_id, cookie->debug_id, 1,
49 cachefiles_obj_new);
50 return object;
51}
52
53/*
54 * Note that an object has been seen.
55 */
56void cachefiles_see_object(struct cachefiles_object *object,
57 enum cachefiles_obj_ref_trace why)
58{
59 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id,
60 refcount_read(&object->ref), why);
61}
62
63/*
64 * Increment the usage count on an object;
65 */
66struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
67 enum cachefiles_obj_ref_trace why)
68{
69 int r;
70
71 __refcount_inc(&object->ref, &r);
72 trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, r, why);
73 return object;
74}
75
76/*
77 * dispose of a reference to an object
78 */
79void cachefiles_put_object(struct cachefiles_object *object,
80 enum cachefiles_obj_ref_trace why)
81{
82 unsigned int object_debug_id = object->debug_id;
83 unsigned int cookie_debug_id = object->cookie->debug_id;
84 struct fscache_cache *cache;
85 bool done;
86 int r;
87
88 done = __refcount_dec_and_test(&object->ref, &r);
89 trace_cachefiles_ref(object_debug_id, cookie_debug_id, r, why);
90 if (done) {
91 _debug("- kill object OBJ%x", object_debug_id);
92
93 ASSERTCMP(object->file, ==, NULL);
94
95 kfree(object->d_name);
96 cachefiles_ondemand_deinit_obj_info(object);
97 cache = object->volume->cache->cache;
98 fscache_put_cookie(object->cookie, fscache_cookie_put_object);
99 object->cookie = NULL;
100 kmem_cache_free(cachefiles_object_jar, object);
101 fscache_uncount_object(cache);
102 }
103
104 _leave("");
105}
106
107/*
108 * Adjust the size of a cache file if necessary to match the DIO size. We keep
109 * the EOF marker a multiple of DIO blocks so that we don't fall back to doing
110 * non-DIO for a partial block straddling the EOF, but we also have to be
111 * careful of someone expanding the file and accidentally accreting the
112 * padding.
113 */
114static int cachefiles_adjust_size(struct cachefiles_object *object)
115{
116 struct iattr newattrs;
117 struct file *file = object->file;
118 uint64_t ni_size;
119 loff_t oi_size;
120 int ret;
121
122 ni_size = object->cookie->object_size;
123 ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
124
125 _enter("{OBJ%x},[%llu]",
126 object->debug_id, (unsigned long long) ni_size);
127
128 if (!file)
129 return -ENOBUFS;
130
131 oi_size = i_size_read(file_inode(file));
132 if (oi_size == ni_size)
133 return 0;
134
135 inode_lock(file_inode(file));
136
137 /* if there's an extension to a partial page at the end of the backing
138 * file, we need to discard the partial page so that we pick up new
139 * data after it */
140 if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
141 _debug("discard tail %llx", oi_size);
142 newattrs.ia_valid = ATTR_SIZE;
143 newattrs.ia_size = oi_size & PAGE_MASK;
144 ret = cachefiles_inject_remove_error();
145 if (ret == 0)
146 ret = notify_change(&nop_mnt_idmap, file->f_path.dentry,
147 &newattrs, NULL);
148 if (ret < 0)
149 goto truncate_failed;
150 }
151
152 newattrs.ia_valid = ATTR_SIZE;
153 newattrs.ia_size = ni_size;
154 ret = cachefiles_inject_write_error();
155 if (ret == 0)
156 ret = notify_change(&nop_mnt_idmap, file->f_path.dentry,
157 &newattrs, NULL);
158
159truncate_failed:
160 inode_unlock(file_inode(file));
161
162 if (ret < 0)
163 trace_cachefiles_io_error(NULL, file_inode(file), ret,
164 cachefiles_trace_notify_change_error);
165 if (ret == -EIO) {
166 cachefiles_io_error_obj(object, "Size set failed");
167 ret = -ENOBUFS;
168 }
169
170 _leave(" = %d", ret);
171 return ret;
172}
173
174/*
175 * Attempt to look up the nominated node in this cache
176 */
177static bool cachefiles_lookup_cookie(struct fscache_cookie *cookie)
178{
179 struct cachefiles_object *object;
180 struct cachefiles_cache *cache = cookie->volume->cache->cache_priv;
181 const struct cred *saved_cred;
182 bool success;
183
184 object = cachefiles_alloc_object(cookie);
185 if (!object)
186 goto fail;
187
188 _enter("{OBJ%x}", object->debug_id);
189
190 if (!cachefiles_cook_key(object))
191 goto fail_put;
192
193 cookie->cache_priv = object;
194
195 cachefiles_begin_secure(cache, &saved_cred);
196
197 success = cachefiles_look_up_object(object);
198 if (!success)
199 goto fail_withdraw;
200
201 cachefiles_see_object(object, cachefiles_obj_see_lookup_cookie);
202
203 spin_lock(&cache->object_list_lock);
204 list_add(&object->cache_link, &cache->object_list);
205 spin_unlock(&cache->object_list_lock);
206 cachefiles_adjust_size(object);
207
208 cachefiles_end_secure(cache, saved_cred);
209 _leave(" = t");
210 return true;
211
212fail_withdraw:
213 cachefiles_end_secure(cache, saved_cred);
214 cachefiles_see_object(object, cachefiles_obj_see_lookup_failed);
215 fscache_caching_failed(cookie);
216 _debug("failed c=%08x o=%08x", cookie->debug_id, object->debug_id);
217 /* The caller holds an access count on the cookie, so we need them to
218 * drop it before we can withdraw the object.
219 */
220 return false;
221
222fail_put:
223 cachefiles_put_object(object, cachefiles_obj_put_alloc_fail);
224fail:
225 return false;
226}
227
228/*
229 * Shorten the backing object to discard any dirty data and free up
230 * any unused granules.
231 */
232static bool cachefiles_shorten_object(struct cachefiles_object *object,
233 struct file *file, loff_t new_size)
234{
235 struct cachefiles_cache *cache = object->volume->cache;
236 struct inode *inode = file_inode(file);
237 loff_t i_size, dio_size;
238 int ret;
239
240 dio_size = round_up(new_size, CACHEFILES_DIO_BLOCK_SIZE);
241 i_size = i_size_read(inode);
242
243 trace_cachefiles_trunc(object, inode, i_size, dio_size,
244 cachefiles_trunc_shrink);
245 ret = cachefiles_inject_remove_error();
246 if (ret == 0)
247 ret = vfs_truncate(&file->f_path, dio_size);
248 if (ret < 0) {
249 trace_cachefiles_io_error(object, file_inode(file), ret,
250 cachefiles_trace_trunc_error);
251 cachefiles_io_error_obj(object, "Trunc-to-size failed %d", ret);
252 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
253 return false;
254 }
255
256 if (new_size < dio_size) {
257 trace_cachefiles_trunc(object, inode, dio_size, new_size,
258 cachefiles_trunc_dio_adjust);
259 ret = cachefiles_inject_write_error();
260 if (ret == 0)
261 ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE,
262 new_size, dio_size - new_size);
263 if (ret < 0) {
264 trace_cachefiles_io_error(object, file_inode(file), ret,
265 cachefiles_trace_fallocate_error);
266 cachefiles_io_error_obj(object, "Trunc-to-dio-size failed %d", ret);
267 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
268 return false;
269 }
270 }
271
272 return true;
273}
274
275/*
276 * Resize the backing object.
277 */
278static void cachefiles_resize_cookie(struct netfs_cache_resources *cres,
279 loff_t new_size)
280{
281 struct cachefiles_object *object = cachefiles_cres_object(cres);
282 struct cachefiles_cache *cache = object->volume->cache;
283 struct fscache_cookie *cookie = object->cookie;
284 const struct cred *saved_cred;
285 struct file *file = cachefiles_cres_file(cres);
286 loff_t old_size = cookie->object_size;
287
288 _enter("%llu->%llu", old_size, new_size);
289
290 if (new_size < old_size) {
291 cachefiles_begin_secure(cache, &saved_cred);
292 cachefiles_shorten_object(object, file, new_size);
293 cachefiles_end_secure(cache, saved_cred);
294 object->cookie->object_size = new_size;
295 return;
296 }
297
298 /* The file is being expanded. We don't need to do anything
299 * particularly. cookie->initial_size doesn't change and so the point
300 * at which we have to download before doesn't change.
301 */
302 cookie->object_size = new_size;
303}
304
305/*
306 * Commit changes to the object as we drop it.
307 */
308static void cachefiles_commit_object(struct cachefiles_object *object,
309 struct cachefiles_cache *cache)
310{
311 bool update = false;
312
313 if (test_and_clear_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
314 update = true;
315 if (test_and_clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags))
316 update = true;
317 if (update)
318 cachefiles_set_object_xattr(object);
319
320 if (test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags))
321 cachefiles_commit_tmpfile(cache, object);
322}
323
324/*
325 * Finalise and object and close the VFS structs that we have.
326 */
327static void cachefiles_clean_up_object(struct cachefiles_object *object,
328 struct cachefiles_cache *cache)
329{
330 if (test_bit(FSCACHE_COOKIE_RETIRED, &object->cookie->flags)) {
331 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
332 cachefiles_see_object(object, cachefiles_obj_see_clean_delete);
333 _debug("- inval object OBJ%x", object->debug_id);
334 cachefiles_delete_object(object, FSCACHE_OBJECT_WAS_RETIRED);
335 } else {
336 cachefiles_see_object(object, cachefiles_obj_see_clean_drop_tmp);
337 _debug("- inval object OBJ%x tmpfile", object->debug_id);
338 }
339 } else {
340 cachefiles_see_object(object, cachefiles_obj_see_clean_commit);
341 cachefiles_commit_object(object, cache);
342 }
343
344 cachefiles_unmark_inode_in_use(object, object->file);
345 if (object->file) {
346 fput(object->file);
347 object->file = NULL;
348 }
349}
350
351/*
352 * Withdraw caching for a cookie.
353 */
354static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie)
355{
356 struct cachefiles_object *object = cookie->cache_priv;
357 struct cachefiles_cache *cache = object->volume->cache;
358 const struct cred *saved_cred;
359
360 _enter("o=%x", object->debug_id);
361 cachefiles_see_object(object, cachefiles_obj_see_withdraw_cookie);
362
363 if (!list_empty(&object->cache_link)) {
364 spin_lock(&cache->object_list_lock);
365 cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
366 list_del_init(&object->cache_link);
367 spin_unlock(&cache->object_list_lock);
368 }
369
370 cachefiles_ondemand_clean_object(object);
371
372 if (object->file) {
373 cachefiles_begin_secure(cache, &saved_cred);
374 cachefiles_clean_up_object(object, cache);
375 cachefiles_end_secure(cache, saved_cred);
376 }
377
378 cookie->cache_priv = NULL;
379 cachefiles_put_object(object, cachefiles_obj_put_detach);
380}
381
382/*
383 * Invalidate the storage associated with a cookie.
384 */
385static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
386{
387 struct cachefiles_object *object = cookie->cache_priv;
388 struct file *new_file, *old_file;
389 bool old_tmpfile;
390
391 _enter("o=%x,[%llu]", object->debug_id, object->cookie->object_size);
392
393 old_tmpfile = test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
394
395 if (!object->file) {
396 fscache_resume_after_invalidation(cookie);
397 _leave(" = t [light]");
398 return true;
399 }
400
401 new_file = cachefiles_create_tmpfile(object);
402 if (IS_ERR(new_file))
403 goto failed;
404
405 /* Substitute the VFS target */
406 _debug("sub");
407 spin_lock(&object->lock);
408
409 old_file = object->file;
410 object->file = new_file;
411 object->content_info = CACHEFILES_CONTENT_NO_DATA;
412 set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
413 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
414
415 spin_unlock(&object->lock);
416 _debug("subbed");
417
418 /* Allow I/O to take place again */
419 fscache_resume_after_invalidation(cookie);
420
421 if (old_file) {
422 if (!old_tmpfile) {
423 struct cachefiles_volume *volume = object->volume;
424 struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
425
426 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
427 cachefiles_bury_object(volume->cache, object, fan,
428 old_file->f_path.dentry,
429 FSCACHE_OBJECT_INVALIDATED);
430 }
431 fput(old_file);
432 }
433
434 _leave(" = t");
435 return true;
436
437failed:
438 _leave(" = f");
439 return false;
440}
441
442const struct fscache_cache_ops cachefiles_cache_ops = {
443 .name = "cachefiles",
444 .acquire_volume = cachefiles_acquire_volume,
445 .free_volume = cachefiles_free_volume,
446 .lookup_cookie = cachefiles_lookup_cookie,
447 .withdraw_cookie = cachefiles_withdraw_cookie,
448 .invalidate_cookie = cachefiles_invalidate_cookie,
449 .begin_operation = cachefiles_begin_operation,
450 .resize_cookie = cachefiles_resize_cookie,
451 .prepare_to_write = cachefiles_prepare_to_write,
452};
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/slab.h>
13#include <linux/mount.h>
14#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
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
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
117 * - return -ETIMEDOUT to be scheduled again
118 */
119static 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 if (ret != -ENOBUFS)
151 printk(KERN_WARNING
152 "CacheFiles: Lookup failed error %d\n", ret);
153 fscache_object_lookup_error(&object->fscache);
154 }
155
156 _leave(" [%d]", ret);
157 return ret;
158}
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/*
199 * update the auxiliary data for an object object on disk
200 */
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);
215 cookie = object->fscache.cookie;
216
217 if (!cookie->def->get_aux) {
218 _leave(" [no aux]");
219 return;
220 }
221
222 auxdata = kmalloc(2 + 512 + 3, GFP_KERNEL);
223 if (!auxdata) {
224 _leave(" [nomem]");
225 return;
226 }
227
228 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
229 ASSERTCMP(auxlen, <, 511);
230
231 auxdata->len = auxlen + 1;
232 auxdata->type = cookie->def->type;
233
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("");
239}
240
241/*
242 * discard the resources pinned by an object and effect retirement if
243 * requested
244 */
245static void cachefiles_drop_object(struct fscache_object *_object)
246{
247 struct cachefiles_object *object;
248 struct cachefiles_cache *cache;
249 const struct cred *saved_cred;
250
251 ASSERT(_object);
252
253 object = container_of(_object, struct cachefiles_object, fscache);
254
255 _enter("{OBJ%x,%d}",
256 object->fscache.debug_id, atomic_read(&object->usage));
257
258 cache = container_of(object->fscache.cache,
259 struct cachefiles_cache, cache);
260
261#ifdef CACHEFILES_DEBUG_SLAB
262 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
263#endif
264
265 /* delete retired objects */
266 if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
267 _object != cache->cache.fsdef
268 ) {
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);
273 }
274
275 /* close the filesystem stuff attached to the object */
276 if (object->backer != object->dentry)
277 dput(object->backer);
278 object->backer = NULL;
279
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);
289 }
290
291 dput(object->dentry);
292 object->dentry = NULL;
293
294 _leave("");
295}
296
297/*
298 * dispose of a reference to an object
299 */
300static void cachefiles_put_object(struct fscache_object *_object)
301{
302 struct cachefiles_object *object;
303 struct fscache_cache *cache;
304
305 ASSERT(_object);
306
307 object = container_of(_object, struct cachefiles_object, fscache);
308
309 _enter("{OBJ%x,%d}",
310 object->fscache.debug_id, atomic_read(&object->usage));
311
312#ifdef CACHEFILES_DEBUG_SLAB
313 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
314#endif
315
316 ASSERTIFCMP(object->fscache.parent,
317 object->fscache.parent->n_children, >, 0);
318
319 if (atomic_dec_and_test(&object->usage)) {
320 _debug("- kill object OBJ%x", object->fscache.debug_id);
321
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);
328
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;
334 }
335
336 cache = object->fscache.cache;
337 fscache_object_destroy(&object->fscache);
338 kmem_cache_free(cachefiles_object_jar, object);
339 fscache_object_destroyed(cache);
340 }
341
342 _leave("");
343}
344
345/*
346 * sync a cache
347 */
348static void cachefiles_sync_cache(struct fscache_cache *_cache)
349{
350 struct cachefiles_cache *cache;
351 const struct cred *saved_cred;
352 int ret;
353
354 _enter("%p", _cache);
355
356 cache = container_of(_cache, struct cachefiles_cache, cache);
357
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);
365
366 if (ret == -EIO)
367 cachefiles_io_error(cache,
368 "Attempt to sync backing fs superblock"
369 " returned error %d",
370 ret);
371}
372
373/*
374 * notification the attributes on an object have changed
375 * - called with reads/writes excluded by FS-Cache
376 */
377static int cachefiles_attr_changed(struct fscache_object *_object)
378{
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;
386
387 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
388
389 _enter("{OBJ%x},[%llu]",
390 _object->debug_id, (unsigned long long) ni_size);
391
392 object = container_of(_object, struct cachefiles_object, fscache);
393 cache = container_of(object->fscache.cache,
394 struct cachefiles_cache, cache);
395
396 if (ni_size == object->i_size)
397 return 0;
398
399 if (!object->backer)
400 return -ENOBUFS;
401
402 ASSERT(S_ISREG(object->backer->d_inode->i_mode));
403
404 fscache_set_store_limit(&object->fscache, ni_size);
405
406 oi_size = i_size_read(object->backer->d_inode);
407 if (oi_size == ni_size)
408 return 0;
409
410 cachefiles_begin_secure(cache, &saved_cred);
411 mutex_lock(&object->backer->d_inode->i_mutex);
412
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;
423 }
424
425 newattrs.ia_valid = ATTR_SIZE;
426 newattrs.ia_size = ni_size;
427 ret = notify_change(object->backer, &newattrs);
428
429truncate_failed:
430 mutex_unlock(&object->backer->d_inode->i_mutex);
431 cachefiles_end_secure(cache, saved_cred);
432
433 if (ret == -EIO) {
434 fscache_set_store_limit(&object->fscache, 0);
435 cachefiles_io_error_obj(object, "Size set failed");
436 ret = -ENOBUFS;
437 }
438
439 _leave(" = %d", ret);
440 return ret;
441}
442
443/*
444 * dissociate a cache from all the pages it was backing
445 */
446static void cachefiles_dissociate_pages(struct fscache_cache *cache)
447{
448 _enter("");
449}
450
451const 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,
469};