|
10 | 10 | #include <linux/namei.h>
|
11 | 11 | #include "internal.h"
|
12 | 12 |
|
| 13 | +/* |
| 14 | + * Bring a cache online. |
| 15 | + */ |
| 16 | +int cachefiles_add_cache(struct cachefiles_cache *cache) |
| 17 | +{ |
| 18 | + struct fscache_cache *cache_cookie; |
| 19 | + struct path path; |
| 20 | + struct kstatfs stats; |
| 21 | + struct dentry *graveyard, *cachedir, *root; |
| 22 | + const struct cred *saved_cred; |
| 23 | + int ret; |
| 24 | + |
| 25 | + _enter(""); |
| 26 | + |
| 27 | + cache_cookie = fscache_acquire_cache(cache->tag); |
| 28 | + if (IS_ERR(cache_cookie)) |
| 29 | + return PTR_ERR(cache_cookie); |
| 30 | + |
| 31 | + /* we want to work under the module's security ID */ |
| 32 | + ret = cachefiles_get_security_ID(cache); |
| 33 | + if (ret < 0) |
| 34 | + goto error_getsec; |
| 35 | + |
| 36 | + cachefiles_begin_secure(cache, &saved_cred); |
| 37 | + |
| 38 | + /* look up the directory at the root of the cache */ |
| 39 | + ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path); |
| 40 | + if (ret < 0) |
| 41 | + goto error_open_root; |
| 42 | + |
| 43 | + cache->mnt = path.mnt; |
| 44 | + root = path.dentry; |
| 45 | + |
| 46 | + ret = -EINVAL; |
| 47 | + if (mnt_user_ns(path.mnt) != &init_user_ns) { |
| 48 | + pr_warn("File cache on idmapped mounts not supported"); |
| 49 | + goto error_unsupported; |
| 50 | + } |
| 51 | + |
| 52 | + /* check parameters */ |
| 53 | + ret = -EOPNOTSUPP; |
| 54 | + if (d_is_negative(root) || |
| 55 | + !d_backing_inode(root)->i_op->lookup || |
| 56 | + !d_backing_inode(root)->i_op->mkdir || |
| 57 | + !(d_backing_inode(root)->i_opflags & IOP_XATTR) || |
| 58 | + !root->d_sb->s_op->statfs || |
| 59 | + !root->d_sb->s_op->sync_fs || |
| 60 | + root->d_sb->s_blocksize > PAGE_SIZE) |
| 61 | + goto error_unsupported; |
| 62 | + |
| 63 | + ret = -EROFS; |
| 64 | + if (sb_rdonly(root->d_sb)) |
| 65 | + goto error_unsupported; |
| 66 | + |
| 67 | + /* determine the security of the on-disk cache as this governs |
| 68 | + * security ID of files we create */ |
| 69 | + ret = cachefiles_determine_cache_security(cache, root, &saved_cred); |
| 70 | + if (ret < 0) |
| 71 | + goto error_unsupported; |
| 72 | + |
| 73 | + /* get the cache size and blocksize */ |
| 74 | + ret = vfs_statfs(&path, &stats); |
| 75 | + if (ret < 0) |
| 76 | + goto error_unsupported; |
| 77 | + |
| 78 | + ret = -ERANGE; |
| 79 | + if (stats.f_bsize <= 0) |
| 80 | + goto error_unsupported; |
| 81 | + |
| 82 | + ret = -EOPNOTSUPP; |
| 83 | + if (stats.f_bsize > PAGE_SIZE) |
| 84 | + goto error_unsupported; |
| 85 | + |
| 86 | + cache->bsize = stats.f_bsize; |
| 87 | + cache->bshift = 0; |
| 88 | + if (stats.f_bsize < PAGE_SIZE) |
| 89 | + cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize); |
| 90 | + |
| 91 | + _debug("blksize %u (shift %u)", |
| 92 | + cache->bsize, cache->bshift); |
| 93 | + |
| 94 | + _debug("size %llu, avail %llu", |
| 95 | + (unsigned long long) stats.f_blocks, |
| 96 | + (unsigned long long) stats.f_bavail); |
| 97 | + |
| 98 | + /* set up caching limits */ |
| 99 | + do_div(stats.f_files, 100); |
| 100 | + cache->fstop = stats.f_files * cache->fstop_percent; |
| 101 | + cache->fcull = stats.f_files * cache->fcull_percent; |
| 102 | + cache->frun = stats.f_files * cache->frun_percent; |
| 103 | + |
| 104 | + _debug("limits {%llu,%llu,%llu} files", |
| 105 | + (unsigned long long) cache->frun, |
| 106 | + (unsigned long long) cache->fcull, |
| 107 | + (unsigned long long) cache->fstop); |
| 108 | + |
| 109 | + stats.f_blocks >>= cache->bshift; |
| 110 | + do_div(stats.f_blocks, 100); |
| 111 | + cache->bstop = stats.f_blocks * cache->bstop_percent; |
| 112 | + cache->bcull = stats.f_blocks * cache->bcull_percent; |
| 113 | + cache->brun = stats.f_blocks * cache->brun_percent; |
| 114 | + |
| 115 | + _debug("limits {%llu,%llu,%llu} blocks", |
| 116 | + (unsigned long long) cache->brun, |
| 117 | + (unsigned long long) cache->bcull, |
| 118 | + (unsigned long long) cache->bstop); |
| 119 | + |
| 120 | + /* get the cache directory and check its type */ |
| 121 | + cachedir = cachefiles_get_directory(cache, root, "cache", NULL); |
| 122 | + if (IS_ERR(cachedir)) { |
| 123 | + ret = PTR_ERR(cachedir); |
| 124 | + goto error_unsupported; |
| 125 | + } |
| 126 | + |
| 127 | + cache->store = cachedir; |
| 128 | + |
| 129 | + /* get the graveyard directory */ |
| 130 | + graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL); |
| 131 | + if (IS_ERR(graveyard)) { |
| 132 | + ret = PTR_ERR(graveyard); |
| 133 | + goto error_unsupported; |
| 134 | + } |
| 135 | + |
| 136 | + cache->graveyard = graveyard; |
| 137 | + cache->cache = cache_cookie; |
| 138 | + |
| 139 | + ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache); |
| 140 | + if (ret < 0) |
| 141 | + goto error_add_cache; |
| 142 | + |
| 143 | + /* done */ |
| 144 | + set_bit(CACHEFILES_READY, &cache->flags); |
| 145 | + dput(root); |
| 146 | + |
| 147 | + pr_info("File cache on %s registered\n", cache_cookie->name); |
| 148 | + |
| 149 | + /* check how much space the cache has */ |
| 150 | + cachefiles_has_space(cache, 0, 0); |
| 151 | + cachefiles_end_secure(cache, saved_cred); |
| 152 | + _leave(" = 0 [%px]", cache->cache); |
| 153 | + return 0; |
| 154 | + |
| 155 | +error_add_cache: |
| 156 | + cachefiles_put_directory(cache->graveyard); |
| 157 | + cache->graveyard = NULL; |
| 158 | +error_unsupported: |
| 159 | + cachefiles_put_directory(cache->store); |
| 160 | + cache->store = NULL; |
| 161 | + mntput(cache->mnt); |
| 162 | + cache->mnt = NULL; |
| 163 | + dput(root); |
| 164 | +error_open_root: |
| 165 | + cachefiles_end_secure(cache, saved_cred); |
| 166 | +error_getsec: |
| 167 | + fscache_relinquish_cache(cache_cookie); |
| 168 | + cache->cache = NULL; |
| 169 | + pr_err("Failed to register: %d\n", ret); |
| 170 | + return ret; |
| 171 | +} |
| 172 | + |
13 | 173 | /*
|
14 | 174 | * See if we have space for a number of pages and/or a number of files in the
|
15 | 175 | * cache
|
@@ -101,3 +261,50 @@ int cachefiles_has_space(struct cachefiles_cache *cache,
|
101 | 261 | _leave(" = %d", ret);
|
102 | 262 | return ret;
|
103 | 263 | }
|
| 264 | + |
| 265 | +/* |
| 266 | + * Sync a cache to backing disk. |
| 267 | + */ |
| 268 | +static void cachefiles_sync_cache(struct cachefiles_cache *cache) |
| 269 | +{ |
| 270 | + const struct cred *saved_cred; |
| 271 | + int ret; |
| 272 | + |
| 273 | + _enter("%s", cache->cache->name); |
| 274 | + |
| 275 | + /* make sure all pages pinned by operations on behalf of the netfs are |
| 276 | + * written to disc */ |
| 277 | + cachefiles_begin_secure(cache, &saved_cred); |
| 278 | + down_read(&cache->mnt->mnt_sb->s_umount); |
| 279 | + ret = sync_filesystem(cache->mnt->mnt_sb); |
| 280 | + up_read(&cache->mnt->mnt_sb->s_umount); |
| 281 | + cachefiles_end_secure(cache, saved_cred); |
| 282 | + |
| 283 | + if (ret == -EIO) |
| 284 | + cachefiles_io_error(cache, |
| 285 | + "Attempt to sync backing fs superblock returned error %d", |
| 286 | + ret); |
| 287 | +} |
| 288 | + |
| 289 | +/* |
| 290 | + * Withdraw cache objects. |
| 291 | + */ |
| 292 | +void cachefiles_withdraw_cache(struct cachefiles_cache *cache) |
| 293 | +{ |
| 294 | + struct fscache_cache *fscache = cache->cache; |
| 295 | + |
| 296 | + pr_info("File cache on %s unregistering\n", fscache->name); |
| 297 | + |
| 298 | + fscache_withdraw_cache(fscache); |
| 299 | + |
| 300 | + /* we now have to destroy all the active objects pertaining to this |
| 301 | + * cache - which we do by passing them off to thread pool to be |
| 302 | + * disposed of */ |
| 303 | + // PLACEHOLDER: Withdraw objects |
| 304 | + fscache_wait_for_objects(fscache); |
| 305 | + |
| 306 | + // PLACEHOLDER: Withdraw volume |
| 307 | + cachefiles_sync_cache(cache); |
| 308 | + cache->cache = NULL; |
| 309 | + fscache_relinquish_cache(fscache); |
| 310 | +} |
0 commit comments