xref: /linux/fs/cachefiles/cache.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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 (dhowells@redhat.com)
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 (is_idmapped_mnt(path.mnt)) {
48 		pr_warn("File cache on idmapped mounts not supported");
49 		goto error_unsupported;
50 	}
51 
52 	/* Check features of the backing filesystem:
53 	 * - Directories must support looking up and directory creation
54 	 * - We create tmpfiles to handle invalidation
55 	 * - We use xattrs to store metadata
56 	 * - We need to be able to query the amount of space available
57 	 * - We want to be able to sync the filesystem when stopping the cache
58 	 * - We use DIO to/from pages, so the blocksize mustn't be too big.
59 	 */
60 	ret = -EOPNOTSUPP;
61 	if (d_is_negative(root) ||
62 	    !d_backing_inode(root)->i_op->lookup ||
63 	    !d_backing_inode(root)->i_op->mkdir ||
64 	    !d_backing_inode(root)->i_op->tmpfile ||
65 	    !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
66 	    !root->d_sb->s_op->statfs ||
67 	    !root->d_sb->s_op->sync_fs ||
68 	    root->d_sb->s_blocksize > PAGE_SIZE)
69 		goto error_unsupported;
70 
71 	ret = -EROFS;
72 	if (sb_rdonly(root->d_sb))
73 		goto error_unsupported;
74 
75 	/* determine the security of the on-disk cache as this governs
76 	 * security ID of files we create */
77 	ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
78 	if (ret < 0)
79 		goto error_unsupported;
80 
81 	/* get the cache size and blocksize */
82 	ret = vfs_statfs(&path, &stats);
83 	if (ret < 0)
84 		goto error_unsupported;
85 
86 	ret = -ERANGE;
87 	if (stats.f_bsize <= 0)
88 		goto error_unsupported;
89 
90 	ret = -EOPNOTSUPP;
91 	if (stats.f_bsize > PAGE_SIZE)
92 		goto error_unsupported;
93 
94 	cache->bsize = stats.f_bsize;
95 	cache->bshift = ilog2(stats.f_bsize);
96 
97 	_debug("blksize %u (shift %u)",
98 	       cache->bsize, cache->bshift);
99 
100 	_debug("size %llu, avail %llu",
101 	       (unsigned long long) stats.f_blocks,
102 	       (unsigned long long) stats.f_bavail);
103 
104 	/* set up caching limits */
105 	do_div(stats.f_files, 100);
106 	cache->fstop = stats.f_files * cache->fstop_percent;
107 	cache->fcull = stats.f_files * cache->fcull_percent;
108 	cache->frun  = stats.f_files * cache->frun_percent;
109 
110 	_debug("limits {%llu,%llu,%llu} files",
111 	       (unsigned long long) cache->frun,
112 	       (unsigned long long) cache->fcull,
113 	       (unsigned long long) cache->fstop);
114 
115 	do_div(stats.f_blocks, 100);
116 	cache->bstop = stats.f_blocks * cache->bstop_percent;
117 	cache->bcull = stats.f_blocks * cache->bcull_percent;
118 	cache->brun  = stats.f_blocks * cache->brun_percent;
119 
120 	_debug("limits {%llu,%llu,%llu} blocks",
121 	       (unsigned long long) cache->brun,
122 	       (unsigned long long) cache->bcull,
123 	       (unsigned long long) cache->bstop);
124 
125 	/* get the cache directory and check its type */
126 	cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
127 	if (IS_ERR(cachedir)) {
128 		ret = PTR_ERR(cachedir);
129 		goto error_unsupported;
130 	}
131 
132 	cache->store = cachedir;
133 
134 	/* get the graveyard directory */
135 	graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
136 	if (IS_ERR(graveyard)) {
137 		ret = PTR_ERR(graveyard);
138 		goto error_unsupported;
139 	}
140 
141 	cache->graveyard = graveyard;
142 	cache->cache = cache_cookie;
143 
144 	ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
145 	if (ret < 0)
146 		goto error_add_cache;
147 
148 	/* done */
149 	set_bit(CACHEFILES_READY, &cache->flags);
150 	dput(root);
151 
152 	pr_info("File cache on %s registered\n", cache_cookie->name);
153 
154 	/* check how much space the cache has */
155 	cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
156 	cachefiles_end_secure(cache, saved_cred);
157 	_leave(" = 0 [%px]", cache->cache);
158 	return 0;
159 
160 error_add_cache:
161 	cachefiles_put_directory(cache->graveyard);
162 	cache->graveyard = NULL;
163 error_unsupported:
164 	cachefiles_put_directory(cache->store);
165 	cache->store = NULL;
166 	mntput(cache->mnt);
167 	cache->mnt = NULL;
168 	dput(root);
169 error_open_root:
170 	cachefiles_end_secure(cache, saved_cred);
171 	put_cred(cache->cache_cred);
172 	cache->cache_cred = NULL;
173 error_getsec:
174 	fscache_relinquish_cache(cache_cookie);
175 	cache->cache = NULL;
176 	pr_err("Failed to register: %d\n", ret);
177 	return ret;
178 }
179 
180 /*
181  * See if we have space for a number of pages and/or a number of files in the
182  * cache
183  */
184 int cachefiles_has_space(struct cachefiles_cache *cache,
185 			 unsigned fnr, unsigned bnr,
186 			 enum cachefiles_has_space_for reason)
187 {
188 	struct kstatfs stats;
189 	u64 b_avail, b_writing;
190 	int ret;
191 
192 	struct path path = {
193 		.mnt	= cache->mnt,
194 		.dentry	= cache->mnt->mnt_root,
195 	};
196 
197 	//_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
198 	//       (unsigned long long) cache->frun,
199 	//       (unsigned long long) cache->fcull,
200 	//       (unsigned long long) cache->fstop,
201 	//       (unsigned long long) cache->brun,
202 	//       (unsigned long long) cache->bcull,
203 	//       (unsigned long long) cache->bstop,
204 	//       fnr, bnr);
205 
206 	/* find out how many pages of blockdev are available */
207 	memset(&stats, 0, sizeof(stats));
208 
209 	ret = vfs_statfs(&path, &stats);
210 	if (ret < 0) {
211 		trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
212 					   cachefiles_trace_statfs_error);
213 		if (ret == -EIO)
214 			cachefiles_io_error(cache, "statfs failed");
215 		_leave(" = %d", ret);
216 		return ret;
217 	}
218 
219 	b_avail = stats.f_bavail;
220 	b_writing = atomic_long_read(&cache->b_writing);
221 	if (b_avail > b_writing)
222 		b_avail -= b_writing;
223 	else
224 		b_avail = 0;
225 
226 	//_debug("avail %llu,%llu",
227 	//       (unsigned long long)stats.f_ffree,
228 	//       (unsigned long long)b_avail);
229 
230 	/* see if there is sufficient space */
231 	if (stats.f_ffree > fnr)
232 		stats.f_ffree -= fnr;
233 	else
234 		stats.f_ffree = 0;
235 
236 	if (b_avail > bnr)
237 		b_avail -= bnr;
238 	else
239 		b_avail = 0;
240 
241 	ret = -ENOBUFS;
242 	if (stats.f_ffree < cache->fstop ||
243 	    b_avail < cache->bstop)
244 		goto stop_and_begin_cull;
245 
246 	ret = 0;
247 	if (stats.f_ffree < cache->fcull ||
248 	    b_avail < cache->bcull)
249 		goto begin_cull;
250 
251 	if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
252 	    stats.f_ffree >= cache->frun &&
253 	    b_avail >= cache->brun &&
254 	    test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
255 	    ) {
256 		_debug("cease culling");
257 		cachefiles_state_changed(cache);
258 	}
259 
260 	//_leave(" = 0");
261 	return 0;
262 
263 stop_and_begin_cull:
264 	switch (reason) {
265 	case cachefiles_has_space_for_write:
266 		fscache_count_no_write_space();
267 		break;
268 	case cachefiles_has_space_for_create:
269 		fscache_count_no_create_space();
270 		break;
271 	default:
272 		break;
273 	}
274 begin_cull:
275 	if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
276 		_debug("### CULL CACHE ###");
277 		cachefiles_state_changed(cache);
278 	}
279 
280 	_leave(" = %d", ret);
281 	return ret;
282 }
283 
284 /*
285  * Mark all the objects as being out of service and queue them all for cleanup.
286  */
287 static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
288 {
289 	struct cachefiles_object *object;
290 	unsigned int count = 0;
291 
292 	_enter("");
293 
294 	spin_lock(&cache->object_list_lock);
295 
296 	while (!list_empty(&cache->object_list)) {
297 		object = list_first_entry(&cache->object_list,
298 					  struct cachefiles_object, cache_link);
299 		cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
300 		list_del_init(&object->cache_link);
301 		fscache_withdraw_cookie(object->cookie);
302 		count++;
303 		if ((count & 63) == 0) {
304 			spin_unlock(&cache->object_list_lock);
305 			cond_resched();
306 			spin_lock(&cache->object_list_lock);
307 		}
308 	}
309 
310 	spin_unlock(&cache->object_list_lock);
311 	_leave(" [%u objs]", count);
312 }
313 
314 /*
315  * Withdraw volumes.
316  */
317 static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
318 {
319 	_enter("");
320 
321 	for (;;) {
322 		struct cachefiles_volume *volume = NULL;
323 
324 		spin_lock(&cache->object_list_lock);
325 		if (!list_empty(&cache->volumes)) {
326 			volume = list_first_entry(&cache->volumes,
327 						  struct cachefiles_volume, cache_link);
328 			list_del_init(&volume->cache_link);
329 		}
330 		spin_unlock(&cache->object_list_lock);
331 		if (!volume)
332 			break;
333 
334 		cachefiles_withdraw_volume(volume);
335 	}
336 
337 	_leave("");
338 }
339 
340 /*
341  * Sync a cache to backing disk.
342  */
343 static void cachefiles_sync_cache(struct cachefiles_cache *cache)
344 {
345 	const struct cred *saved_cred;
346 	int ret;
347 
348 	_enter("%s", cache->cache->name);
349 
350 	/* make sure all pages pinned by operations on behalf of the netfs are
351 	 * written to disc */
352 	cachefiles_begin_secure(cache, &saved_cred);
353 	down_read(&cache->mnt->mnt_sb->s_umount);
354 	ret = sync_filesystem(cache->mnt->mnt_sb);
355 	up_read(&cache->mnt->mnt_sb->s_umount);
356 	cachefiles_end_secure(cache, saved_cred);
357 
358 	if (ret == -EIO)
359 		cachefiles_io_error(cache,
360 				    "Attempt to sync backing fs superblock returned error %d",
361 				    ret);
362 }
363 
364 /*
365  * Withdraw cache objects.
366  */
367 void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
368 {
369 	struct fscache_cache *fscache = cache->cache;
370 
371 	pr_info("File cache on %s unregistering\n", fscache->name);
372 
373 	fscache_withdraw_cache(fscache);
374 
375 	/* we now have to destroy all the active objects pertaining to this
376 	 * cache - which we do by passing them off to thread pool to be
377 	 * disposed of */
378 	cachefiles_withdraw_objects(cache);
379 	fscache_wait_for_objects(fscache);
380 
381 	cachefiles_withdraw_volumes(cache);
382 	cachefiles_sync_cache(cache);
383 	cache->cache = NULL;
384 	fscache_relinquish_cache(fscache);
385 }
386