Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* FS-Cache interface to CacheFiles
0003  *
0004  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/mount.h>
0010 #include <linux/xattr.h>
0011 #include <linux/file.h>
0012 #include <linux/falloc.h>
0013 #include <trace/events/fscache.h>
0014 #include "internal.h"
0015 
0016 static atomic_t cachefiles_object_debug_id;
0017 
0018 /*
0019  * Allocate a cache object record.
0020  */
0021 static
0022 struct cachefiles_object *cachefiles_alloc_object(struct fscache_cookie *cookie)
0023 {
0024     struct fscache_volume *vcookie = cookie->volume;
0025     struct cachefiles_volume *volume = vcookie->cache_priv;
0026     struct cachefiles_object *object;
0027 
0028     _enter("{%s},%x,", vcookie->key, cookie->debug_id);
0029 
0030     object = kmem_cache_zalloc(cachefiles_object_jar, GFP_KERNEL);
0031     if (!object)
0032         return NULL;
0033 
0034     refcount_set(&object->ref, 1);
0035 
0036     spin_lock_init(&object->lock);
0037     INIT_LIST_HEAD(&object->cache_link);
0038     object->volume = volume;
0039     object->debug_id = atomic_inc_return(&cachefiles_object_debug_id);
0040     object->cookie = fscache_get_cookie(cookie, fscache_cookie_get_attach_object);
0041 
0042     fscache_count_object(vcookie->cache);
0043     trace_cachefiles_ref(object->debug_id, cookie->debug_id, 1,
0044                  cachefiles_obj_new);
0045     return object;
0046 }
0047 
0048 /*
0049  * Note that an object has been seen.
0050  */
0051 void cachefiles_see_object(struct cachefiles_object *object,
0052                enum cachefiles_obj_ref_trace why)
0053 {
0054     trace_cachefiles_ref(object->debug_id, object->cookie->debug_id,
0055                  refcount_read(&object->ref), why);
0056 }
0057 
0058 /*
0059  * Increment the usage count on an object;
0060  */
0061 struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
0062                          enum cachefiles_obj_ref_trace why)
0063 {
0064     int r;
0065 
0066     __refcount_inc(&object->ref, &r);
0067     trace_cachefiles_ref(object->debug_id, object->cookie->debug_id, r, why);
0068     return object;
0069 }
0070 
0071 /*
0072  * dispose of a reference to an object
0073  */
0074 void cachefiles_put_object(struct cachefiles_object *object,
0075                enum cachefiles_obj_ref_trace why)
0076 {
0077     unsigned int object_debug_id = object->debug_id;
0078     unsigned int cookie_debug_id = object->cookie->debug_id;
0079     struct fscache_cache *cache;
0080     bool done;
0081     int r;
0082 
0083     done = __refcount_dec_and_test(&object->ref, &r);
0084     trace_cachefiles_ref(object_debug_id, cookie_debug_id, r, why);
0085     if (done) {
0086         _debug("- kill object OBJ%x", object_debug_id);
0087 
0088         ASSERTCMP(object->file, ==, NULL);
0089 
0090         kfree(object->d_name);
0091 
0092         cache = object->volume->cache->cache;
0093         fscache_put_cookie(object->cookie, fscache_cookie_put_object);
0094         object->cookie = NULL;
0095         kmem_cache_free(cachefiles_object_jar, object);
0096         fscache_uncount_object(cache);
0097     }
0098 
0099     _leave("");
0100 }
0101 
0102 /*
0103  * Adjust the size of a cache file if necessary to match the DIO size.  We keep
0104  * the EOF marker a multiple of DIO blocks so that we don't fall back to doing
0105  * non-DIO for a partial block straddling the EOF, but we also have to be
0106  * careful of someone expanding the file and accidentally accreting the
0107  * padding.
0108  */
0109 static int cachefiles_adjust_size(struct cachefiles_object *object)
0110 {
0111     struct iattr newattrs;
0112     struct file *file = object->file;
0113     uint64_t ni_size;
0114     loff_t oi_size;
0115     int ret;
0116 
0117     ni_size = object->cookie->object_size;
0118     ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE);
0119 
0120     _enter("{OBJ%x},[%llu]",
0121            object->debug_id, (unsigned long long) ni_size);
0122 
0123     if (!file)
0124         return -ENOBUFS;
0125 
0126     oi_size = i_size_read(file_inode(file));
0127     if (oi_size == ni_size)
0128         return 0;
0129 
0130     inode_lock(file_inode(file));
0131 
0132     /* if there's an extension to a partial page at the end of the backing
0133      * file, we need to discard the partial page so that we pick up new
0134      * data after it */
0135     if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
0136         _debug("discard tail %llx", oi_size);
0137         newattrs.ia_valid = ATTR_SIZE;
0138         newattrs.ia_size = oi_size & PAGE_MASK;
0139         ret = cachefiles_inject_remove_error();
0140         if (ret == 0)
0141             ret = notify_change(&init_user_ns, file->f_path.dentry,
0142                         &newattrs, NULL);
0143         if (ret < 0)
0144             goto truncate_failed;
0145     }
0146 
0147     newattrs.ia_valid = ATTR_SIZE;
0148     newattrs.ia_size = ni_size;
0149     ret = cachefiles_inject_write_error();
0150     if (ret == 0)
0151         ret = notify_change(&init_user_ns, file->f_path.dentry,
0152                     &newattrs, NULL);
0153 
0154 truncate_failed:
0155     inode_unlock(file_inode(file));
0156 
0157     if (ret < 0)
0158         trace_cachefiles_io_error(NULL, file_inode(file), ret,
0159                       cachefiles_trace_notify_change_error);
0160     if (ret == -EIO) {
0161         cachefiles_io_error_obj(object, "Size set failed");
0162         ret = -ENOBUFS;
0163     }
0164 
0165     _leave(" = %d", ret);
0166     return ret;
0167 }
0168 
0169 /*
0170  * Attempt to look up the nominated node in this cache
0171  */
0172 static bool cachefiles_lookup_cookie(struct fscache_cookie *cookie)
0173 {
0174     struct cachefiles_object *object;
0175     struct cachefiles_cache *cache = cookie->volume->cache->cache_priv;
0176     const struct cred *saved_cred;
0177     bool success;
0178 
0179     object = cachefiles_alloc_object(cookie);
0180     if (!object)
0181         goto fail;
0182 
0183     _enter("{OBJ%x}", object->debug_id);
0184 
0185     if (!cachefiles_cook_key(object))
0186         goto fail_put;
0187 
0188     cookie->cache_priv = object;
0189 
0190     cachefiles_begin_secure(cache, &saved_cred);
0191 
0192     success = cachefiles_look_up_object(object);
0193     if (!success)
0194         goto fail_withdraw;
0195 
0196     cachefiles_see_object(object, cachefiles_obj_see_lookup_cookie);
0197 
0198     spin_lock(&cache->object_list_lock);
0199     list_add(&object->cache_link, &cache->object_list);
0200     spin_unlock(&cache->object_list_lock);
0201     cachefiles_adjust_size(object);
0202 
0203     cachefiles_end_secure(cache, saved_cred);
0204     _leave(" = t");
0205     return true;
0206 
0207 fail_withdraw:
0208     cachefiles_end_secure(cache, saved_cred);
0209     cachefiles_see_object(object, cachefiles_obj_see_lookup_failed);
0210     fscache_caching_failed(cookie);
0211     _debug("failed c=%08x o=%08x", cookie->debug_id, object->debug_id);
0212     /* The caller holds an access count on the cookie, so we need them to
0213      * drop it before we can withdraw the object.
0214      */
0215     return false;
0216 
0217 fail_put:
0218     cachefiles_put_object(object, cachefiles_obj_put_alloc_fail);
0219 fail:
0220     return false;
0221 }
0222 
0223 /*
0224  * Shorten the backing object to discard any dirty data and free up
0225  * any unused granules.
0226  */
0227 static bool cachefiles_shorten_object(struct cachefiles_object *object,
0228                       struct file *file, loff_t new_size)
0229 {
0230     struct cachefiles_cache *cache = object->volume->cache;
0231     struct inode *inode = file_inode(file);
0232     loff_t i_size, dio_size;
0233     int ret;
0234 
0235     dio_size = round_up(new_size, CACHEFILES_DIO_BLOCK_SIZE);
0236     i_size = i_size_read(inode);
0237 
0238     trace_cachefiles_trunc(object, inode, i_size, dio_size,
0239                    cachefiles_trunc_shrink);
0240     ret = cachefiles_inject_remove_error();
0241     if (ret == 0)
0242         ret = vfs_truncate(&file->f_path, dio_size);
0243     if (ret < 0) {
0244         trace_cachefiles_io_error(object, file_inode(file), ret,
0245                       cachefiles_trace_trunc_error);
0246         cachefiles_io_error_obj(object, "Trunc-to-size failed %d", ret);
0247         cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
0248         return false;
0249     }
0250 
0251     if (new_size < dio_size) {
0252         trace_cachefiles_trunc(object, inode, dio_size, new_size,
0253                        cachefiles_trunc_dio_adjust);
0254         ret = cachefiles_inject_write_error();
0255         if (ret == 0)
0256             ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE,
0257                         new_size, dio_size - new_size);
0258         if (ret < 0) {
0259             trace_cachefiles_io_error(object, file_inode(file), ret,
0260                           cachefiles_trace_fallocate_error);
0261             cachefiles_io_error_obj(object, "Trunc-to-dio-size failed %d", ret);
0262             cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
0263             return false;
0264         }
0265     }
0266 
0267     return true;
0268 }
0269 
0270 /*
0271  * Resize the backing object.
0272  */
0273 static void cachefiles_resize_cookie(struct netfs_cache_resources *cres,
0274                      loff_t new_size)
0275 {
0276     struct cachefiles_object *object = cachefiles_cres_object(cres);
0277     struct cachefiles_cache *cache = object->volume->cache;
0278     struct fscache_cookie *cookie = object->cookie;
0279     const struct cred *saved_cred;
0280     struct file *file = cachefiles_cres_file(cres);
0281     loff_t old_size = cookie->object_size;
0282 
0283     _enter("%llu->%llu", old_size, new_size);
0284 
0285     if (new_size < old_size) {
0286         cachefiles_begin_secure(cache, &saved_cred);
0287         cachefiles_shorten_object(object, file, new_size);
0288         cachefiles_end_secure(cache, saved_cred);
0289         object->cookie->object_size = new_size;
0290         return;
0291     }
0292 
0293     /* The file is being expanded.  We don't need to do anything
0294      * particularly.  cookie->initial_size doesn't change and so the point
0295      * at which we have to download before doesn't change.
0296      */
0297     cookie->object_size = new_size;
0298 }
0299 
0300 /*
0301  * Commit changes to the object as we drop it.
0302  */
0303 static void cachefiles_commit_object(struct cachefiles_object *object,
0304                      struct cachefiles_cache *cache)
0305 {
0306     bool update = false;
0307 
0308     if (test_and_clear_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
0309         update = true;
0310     if (test_and_clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags))
0311         update = true;
0312     if (update)
0313         cachefiles_set_object_xattr(object);
0314 
0315     if (test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags))
0316         cachefiles_commit_tmpfile(cache, object);
0317 }
0318 
0319 /*
0320  * Finalise and object and close the VFS structs that we have.
0321  */
0322 static void cachefiles_clean_up_object(struct cachefiles_object *object,
0323                        struct cachefiles_cache *cache)
0324 {
0325     if (test_bit(FSCACHE_COOKIE_RETIRED, &object->cookie->flags)) {
0326         if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
0327             cachefiles_see_object(object, cachefiles_obj_see_clean_delete);
0328             _debug("- inval object OBJ%x", object->debug_id);
0329             cachefiles_delete_object(object, FSCACHE_OBJECT_WAS_RETIRED);
0330         } else {
0331             cachefiles_see_object(object, cachefiles_obj_see_clean_drop_tmp);
0332             _debug("- inval object OBJ%x tmpfile", object->debug_id);
0333         }
0334     } else {
0335         cachefiles_see_object(object, cachefiles_obj_see_clean_commit);
0336         cachefiles_commit_object(object, cache);
0337     }
0338 
0339     cachefiles_unmark_inode_in_use(object, object->file);
0340     if (object->file) {
0341         fput(object->file);
0342         object->file = NULL;
0343     }
0344 }
0345 
0346 /*
0347  * Withdraw caching for a cookie.
0348  */
0349 static void cachefiles_withdraw_cookie(struct fscache_cookie *cookie)
0350 {
0351     struct cachefiles_object *object = cookie->cache_priv;
0352     struct cachefiles_cache *cache = object->volume->cache;
0353     const struct cred *saved_cred;
0354 
0355     _enter("o=%x", object->debug_id);
0356     cachefiles_see_object(object, cachefiles_obj_see_withdraw_cookie);
0357 
0358     if (!list_empty(&object->cache_link)) {
0359         spin_lock(&cache->object_list_lock);
0360         cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
0361         list_del_init(&object->cache_link);
0362         spin_unlock(&cache->object_list_lock);
0363     }
0364 
0365     cachefiles_ondemand_clean_object(object);
0366 
0367     if (object->file) {
0368         cachefiles_begin_secure(cache, &saved_cred);
0369         cachefiles_clean_up_object(object, cache);
0370         cachefiles_end_secure(cache, saved_cred);
0371     }
0372 
0373     cookie->cache_priv = NULL;
0374     cachefiles_put_object(object, cachefiles_obj_put_detach);
0375 }
0376 
0377 /*
0378  * Invalidate the storage associated with a cookie.
0379  */
0380 static bool cachefiles_invalidate_cookie(struct fscache_cookie *cookie)
0381 {
0382     struct cachefiles_object *object = cookie->cache_priv;
0383     struct file *new_file, *old_file;
0384     bool old_tmpfile;
0385 
0386     _enter("o=%x,[%llu]", object->debug_id, object->cookie->object_size);
0387 
0388     old_tmpfile = test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
0389 
0390     if (!object->file) {
0391         fscache_resume_after_invalidation(cookie);
0392         _leave(" = t [light]");
0393         return true;
0394     }
0395 
0396     new_file = cachefiles_create_tmpfile(object);
0397     if (IS_ERR(new_file))
0398         goto failed;
0399 
0400     /* Substitute the VFS target */
0401     _debug("sub");
0402     spin_lock(&object->lock);
0403 
0404     old_file = object->file;
0405     object->file = new_file;
0406     object->content_info = CACHEFILES_CONTENT_NO_DATA;
0407     set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags);
0408     set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags);
0409 
0410     spin_unlock(&object->lock);
0411     _debug("subbed");
0412 
0413     /* Allow I/O to take place again */
0414     fscache_resume_after_invalidation(cookie);
0415 
0416     if (old_file) {
0417         if (!old_tmpfile) {
0418             struct cachefiles_volume *volume = object->volume;
0419             struct dentry *fan = volume->fanout[(u8)cookie->key_hash];
0420 
0421             inode_lock_nested(d_inode(fan), I_MUTEX_PARENT);
0422             cachefiles_bury_object(volume->cache, object, fan,
0423                            old_file->f_path.dentry,
0424                            FSCACHE_OBJECT_INVALIDATED);
0425         }
0426         fput(old_file);
0427     }
0428 
0429     _leave(" = t");
0430     return true;
0431 
0432 failed:
0433     _leave(" = f");
0434     return false;
0435 }
0436 
0437 const struct fscache_cache_ops cachefiles_cache_ops = {
0438     .name           = "cachefiles",
0439     .acquire_volume     = cachefiles_acquire_volume,
0440     .free_volume        = cachefiles_free_volume,
0441     .lookup_cookie      = cachefiles_lookup_cookie,
0442     .withdraw_cookie    = cachefiles_withdraw_cookie,
0443     .invalidate_cookie  = cachefiles_invalidate_cookie,
0444     .begin_operation    = cachefiles_begin_operation,
0445     .resize_cookie      = cachefiles_resize_cookie,
0446     .prepare_to_write   = cachefiles_prepare_to_write,
0447 };