1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Manage high-level VFS aspects of a cache. 3 * 4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells ([email protected]) 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/statfs.h> 10 #include <linux/namei.h> 11 #include "internal.h" 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 173 /* 174 * See if we have space for a number of pages and/or a number of files in the 175 * cache 176 */ 177 int cachefiles_has_space(struct cachefiles_cache *cache, 178 unsigned fnr, unsigned bnr) 179 { 180 struct kstatfs stats; 181 u64 b_avail, b_writing; 182 int ret; 183 184 struct path path = { 185 .mnt = cache->mnt, 186 .dentry = cache->mnt->mnt_root, 187 }; 188 189 //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u", 190 // (unsigned long long) cache->frun, 191 // (unsigned long long) cache->fcull, 192 // (unsigned long long) cache->fstop, 193 // (unsigned long long) cache->brun, 194 // (unsigned long long) cache->bcull, 195 // (unsigned long long) cache->bstop, 196 // fnr, bnr); 197 198 /* find out how many pages of blockdev are available */ 199 memset(&stats, 0, sizeof(stats)); 200 201 ret = vfs_statfs(&path, &stats); 202 if (ret < 0) { 203 trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret, 204 cachefiles_trace_statfs_error); 205 if (ret == -EIO) 206 cachefiles_io_error(cache, "statfs failed"); 207 _leave(" = %d", ret); 208 return ret; 209 } 210 211 b_avail = stats.f_bavail >> cache->bshift; 212 b_writing = atomic_long_read(&cache->b_writing); 213 if (b_avail > b_writing) 214 b_avail -= b_writing; 215 else 216 b_avail = 0; 217 218 //_debug("avail %llu,%llu", 219 // (unsigned long long)stats.f_ffree, 220 // (unsigned long long)b_avail); 221 222 /* see if there is sufficient space */ 223 if (stats.f_ffree > fnr) 224 stats.f_ffree -= fnr; 225 else 226 stats.f_ffree = 0; 227 228 if (b_avail > bnr) 229 b_avail -= bnr; 230 else 231 b_avail = 0; 232 233 ret = -ENOBUFS; 234 if (stats.f_ffree < cache->fstop || 235 b_avail < cache->bstop) 236 goto begin_cull; 237 238 ret = 0; 239 if (stats.f_ffree < cache->fcull || 240 b_avail < cache->bcull) 241 goto begin_cull; 242 243 if (test_bit(CACHEFILES_CULLING, &cache->flags) && 244 stats.f_ffree >= cache->frun && 245 b_avail >= cache->brun && 246 test_and_clear_bit(CACHEFILES_CULLING, &cache->flags) 247 ) { 248 _debug("cease culling"); 249 cachefiles_state_changed(cache); 250 } 251 252 //_leave(" = 0"); 253 return 0; 254 255 begin_cull: 256 if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) { 257 _debug("### CULL CACHE ###"); 258 cachefiles_state_changed(cache); 259 } 260 261 _leave(" = %d", ret); 262 return ret; 263 } 264 265 /* 266 * Withdraw volumes. 267 */ 268 static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache) 269 { 270 _enter(""); 271 272 for (;;) { 273 struct cachefiles_volume *volume = NULL; 274 275 spin_lock(&cache->object_list_lock); 276 if (!list_empty(&cache->volumes)) { 277 volume = list_first_entry(&cache->volumes, 278 struct cachefiles_volume, cache_link); 279 list_del_init(&volume->cache_link); 280 } 281 spin_unlock(&cache->object_list_lock); 282 if (!volume) 283 break; 284 285 cachefiles_withdraw_volume(volume); 286 } 287 288 _leave(""); 289 } 290 291 /* 292 * Sync a cache to backing disk. 293 */ 294 static void cachefiles_sync_cache(struct cachefiles_cache *cache) 295 { 296 const struct cred *saved_cred; 297 int ret; 298 299 _enter("%s", cache->cache->name); 300 301 /* make sure all pages pinned by operations on behalf of the netfs are 302 * written to disc */ 303 cachefiles_begin_secure(cache, &saved_cred); 304 down_read(&cache->mnt->mnt_sb->s_umount); 305 ret = sync_filesystem(cache->mnt->mnt_sb); 306 up_read(&cache->mnt->mnt_sb->s_umount); 307 cachefiles_end_secure(cache, saved_cred); 308 309 if (ret == -EIO) 310 cachefiles_io_error(cache, 311 "Attempt to sync backing fs superblock returned error %d", 312 ret); 313 } 314 315 /* 316 * Withdraw cache objects. 317 */ 318 void cachefiles_withdraw_cache(struct cachefiles_cache *cache) 319 { 320 struct fscache_cache *fscache = cache->cache; 321 322 pr_info("File cache on %s unregistering\n", fscache->name); 323 324 fscache_withdraw_cache(fscache); 325 326 /* we now have to destroy all the active objects pertaining to this 327 * cache - which we do by passing them off to thread pool to be 328 * disposed of */ 329 // PLACEHOLDER: Withdraw objects 330 fscache_wait_for_objects(fscache); 331 332 cachefiles_withdraw_volumes(cache); 333 cachefiles_sync_cache(cache); 334 cache->cache = NULL; 335 fscache_relinquish_cache(fscache); 336 } 337