xref: /linux/fs/cachefiles/cache.c (revision 83ab4b461eb7bdf90984eb56d4954dbe11e926d4)
180f94f29SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
280f94f29SDavid Howells /* Manage high-level VFS aspects of a cache.
380f94f29SDavid Howells  *
480f94f29SDavid Howells  * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
580f94f29SDavid Howells  * Written by David Howells (dhowells@redhat.com)
680f94f29SDavid Howells  */
780f94f29SDavid Howells 
880f94f29SDavid Howells #include <linux/slab.h>
980f94f29SDavid Howells #include <linux/statfs.h>
1080f94f29SDavid Howells #include <linux/namei.h>
11522018a0SBaokun Li #include <trace/events/fscache.h>
1280f94f29SDavid Howells #include "internal.h"
1380f94f29SDavid Howells 
1480f94f29SDavid Howells /*
15d1065b0aSDavid Howells  * Bring a cache online.
16d1065b0aSDavid Howells  */
cachefiles_add_cache(struct cachefiles_cache * cache)17d1065b0aSDavid Howells int cachefiles_add_cache(struct cachefiles_cache *cache)
18d1065b0aSDavid Howells {
19d1065b0aSDavid Howells 	struct fscache_cache *cache_cookie;
20d1065b0aSDavid Howells 	struct path path;
21d1065b0aSDavid Howells 	struct kstatfs stats;
22d1065b0aSDavid Howells 	struct dentry *graveyard, *cachedir, *root;
23d1065b0aSDavid Howells 	const struct cred *saved_cred;
24d1065b0aSDavid Howells 	int ret;
25d1065b0aSDavid Howells 
26d1065b0aSDavid Howells 	_enter("");
27d1065b0aSDavid Howells 
28d1065b0aSDavid Howells 	cache_cookie = fscache_acquire_cache(cache->tag);
29d1065b0aSDavid Howells 	if (IS_ERR(cache_cookie))
30d1065b0aSDavid Howells 		return PTR_ERR(cache_cookie);
31d1065b0aSDavid Howells 
32d1065b0aSDavid Howells 	/* we want to work under the module's security ID */
33d1065b0aSDavid Howells 	ret = cachefiles_get_security_ID(cache);
34d1065b0aSDavid Howells 	if (ret < 0)
35d1065b0aSDavid Howells 		goto error_getsec;
36d1065b0aSDavid Howells 
37d1065b0aSDavid Howells 	cachefiles_begin_secure(cache, &saved_cred);
38d1065b0aSDavid Howells 
39d1065b0aSDavid Howells 	/* look up the directory at the root of the cache */
40d1065b0aSDavid Howells 	ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
41d1065b0aSDavid Howells 	if (ret < 0)
42d1065b0aSDavid Howells 		goto error_open_root;
43d1065b0aSDavid Howells 
44d1065b0aSDavid Howells 	cache->mnt = path.mnt;
45d1065b0aSDavid Howells 	root = path.dentry;
46d1065b0aSDavid Howells 
47d1065b0aSDavid Howells 	ret = -EINVAL;
488834147fSLinus Torvalds 	if (is_idmapped_mnt(path.mnt)) {
49d1065b0aSDavid Howells 		pr_warn("File cache on idmapped mounts not supported");
50d1065b0aSDavid Howells 		goto error_unsupported;
51d1065b0aSDavid Howells 	}
52d1065b0aSDavid Howells 
5314b9d090SDavid Howells 	/* Check features of the backing filesystem:
5414b9d090SDavid Howells 	 * - Directories must support looking up and directory creation
5566332131SDavid Howells 	 * - We create tmpfiles to handle invalidation
5614b9d090SDavid Howells 	 * - We use xattrs to store metadata
5714b9d090SDavid Howells 	 * - We need to be able to query the amount of space available
5814b9d090SDavid Howells 	 * - We want to be able to sync the filesystem when stopping the cache
5914b9d090SDavid Howells 	 * - We use DIO to/from pages, so the blocksize mustn't be too big.
6014b9d090SDavid Howells 	 */
61d1065b0aSDavid Howells 	ret = -EOPNOTSUPP;
62d1065b0aSDavid Howells 	if (d_is_negative(root) ||
63d1065b0aSDavid Howells 	    !d_backing_inode(root)->i_op->lookup ||
64d1065b0aSDavid Howells 	    !d_backing_inode(root)->i_op->mkdir ||
6566332131SDavid Howells 	    !d_backing_inode(root)->i_op->tmpfile ||
66d1065b0aSDavid Howells 	    !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
67d1065b0aSDavid Howells 	    !root->d_sb->s_op->statfs ||
68d1065b0aSDavid Howells 	    !root->d_sb->s_op->sync_fs ||
69d1065b0aSDavid Howells 	    root->d_sb->s_blocksize > PAGE_SIZE)
70d1065b0aSDavid Howells 		goto error_unsupported;
71d1065b0aSDavid Howells 
72d1065b0aSDavid Howells 	ret = -EROFS;
73d1065b0aSDavid Howells 	if (sb_rdonly(root->d_sb))
74d1065b0aSDavid Howells 		goto error_unsupported;
75d1065b0aSDavid Howells 
76d1065b0aSDavid Howells 	/* determine the security of the on-disk cache as this governs
77d1065b0aSDavid Howells 	 * security ID of files we create */
78d1065b0aSDavid Howells 	ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
79d1065b0aSDavid Howells 	if (ret < 0)
80d1065b0aSDavid Howells 		goto error_unsupported;
81d1065b0aSDavid Howells 
82d1065b0aSDavid Howells 	/* get the cache size and blocksize */
83d1065b0aSDavid Howells 	ret = vfs_statfs(&path, &stats);
84d1065b0aSDavid Howells 	if (ret < 0)
85d1065b0aSDavid Howells 		goto error_unsupported;
86d1065b0aSDavid Howells 
87d1065b0aSDavid Howells 	ret = -ERANGE;
88d1065b0aSDavid Howells 	if (stats.f_bsize <= 0)
89d1065b0aSDavid Howells 		goto error_unsupported;
90d1065b0aSDavid Howells 
91d1065b0aSDavid Howells 	ret = -EOPNOTSUPP;
92d1065b0aSDavid Howells 	if (stats.f_bsize > PAGE_SIZE)
93d1065b0aSDavid Howells 		goto error_unsupported;
94d1065b0aSDavid Howells 
95d1065b0aSDavid Howells 	cache->bsize = stats.f_bsize;
965638b067SDavid Howells 	cache->bshift = ilog2(stats.f_bsize);
97d1065b0aSDavid Howells 
98d1065b0aSDavid Howells 	_debug("blksize %u (shift %u)",
99d1065b0aSDavid Howells 	       cache->bsize, cache->bshift);
100d1065b0aSDavid Howells 
101d1065b0aSDavid Howells 	_debug("size %llu, avail %llu",
102d1065b0aSDavid Howells 	       (unsigned long long) stats.f_blocks,
103d1065b0aSDavid Howells 	       (unsigned long long) stats.f_bavail);
104d1065b0aSDavid Howells 
105d1065b0aSDavid Howells 	/* set up caching limits */
106d1065b0aSDavid Howells 	do_div(stats.f_files, 100);
107d1065b0aSDavid Howells 	cache->fstop = stats.f_files * cache->fstop_percent;
108d1065b0aSDavid Howells 	cache->fcull = stats.f_files * cache->fcull_percent;
109d1065b0aSDavid Howells 	cache->frun  = stats.f_files * cache->frun_percent;
110d1065b0aSDavid Howells 
111d1065b0aSDavid Howells 	_debug("limits {%llu,%llu,%llu} files",
112d1065b0aSDavid Howells 	       (unsigned long long) cache->frun,
113d1065b0aSDavid Howells 	       (unsigned long long) cache->fcull,
114d1065b0aSDavid Howells 	       (unsigned long long) cache->fstop);
115d1065b0aSDavid Howells 
116d1065b0aSDavid Howells 	do_div(stats.f_blocks, 100);
117d1065b0aSDavid Howells 	cache->bstop = stats.f_blocks * cache->bstop_percent;
118d1065b0aSDavid Howells 	cache->bcull = stats.f_blocks * cache->bcull_percent;
119d1065b0aSDavid Howells 	cache->brun  = stats.f_blocks * cache->brun_percent;
120d1065b0aSDavid Howells 
121d1065b0aSDavid Howells 	_debug("limits {%llu,%llu,%llu} blocks",
122d1065b0aSDavid Howells 	       (unsigned long long) cache->brun,
123d1065b0aSDavid Howells 	       (unsigned long long) cache->bcull,
124d1065b0aSDavid Howells 	       (unsigned long long) cache->bstop);
125d1065b0aSDavid Howells 
126d1065b0aSDavid Howells 	/* get the cache directory and check its type */
127d1065b0aSDavid Howells 	cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
128d1065b0aSDavid Howells 	if (IS_ERR(cachedir)) {
129d1065b0aSDavid Howells 		ret = PTR_ERR(cachedir);
130d1065b0aSDavid Howells 		goto error_unsupported;
131d1065b0aSDavid Howells 	}
132d1065b0aSDavid Howells 
133d1065b0aSDavid Howells 	cache->store = cachedir;
134d1065b0aSDavid Howells 
135d1065b0aSDavid Howells 	/* get the graveyard directory */
136d1065b0aSDavid Howells 	graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
137d1065b0aSDavid Howells 	if (IS_ERR(graveyard)) {
138d1065b0aSDavid Howells 		ret = PTR_ERR(graveyard);
139d1065b0aSDavid Howells 		goto error_unsupported;
140d1065b0aSDavid Howells 	}
141d1065b0aSDavid Howells 
142d1065b0aSDavid Howells 	cache->graveyard = graveyard;
143d1065b0aSDavid Howells 	cache->cache = cache_cookie;
144d1065b0aSDavid Howells 
145d1065b0aSDavid Howells 	ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
146d1065b0aSDavid Howells 	if (ret < 0)
147d1065b0aSDavid Howells 		goto error_add_cache;
148d1065b0aSDavid Howells 
149d1065b0aSDavid Howells 	/* done */
150d1065b0aSDavid Howells 	set_bit(CACHEFILES_READY, &cache->flags);
151d1065b0aSDavid Howells 	dput(root);
152d1065b0aSDavid Howells 
153d1065b0aSDavid Howells 	pr_info("File cache on %s registered\n", cache_cookie->name);
154d1065b0aSDavid Howells 
155d1065b0aSDavid Howells 	/* check how much space the cache has */
1563929eca7SDavid Howells 	cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
157d1065b0aSDavid Howells 	cachefiles_end_secure(cache, saved_cred);
158d1065b0aSDavid Howells 	_leave(" = 0 [%px]", cache->cache);
159d1065b0aSDavid Howells 	return 0;
160d1065b0aSDavid Howells 
161d1065b0aSDavid Howells error_add_cache:
162d1065b0aSDavid Howells 	cachefiles_put_directory(cache->graveyard);
163d1065b0aSDavid Howells 	cache->graveyard = NULL;
164d1065b0aSDavid Howells error_unsupported:
165d1065b0aSDavid Howells 	cachefiles_put_directory(cache->store);
166d1065b0aSDavid Howells 	cache->store = NULL;
167d1065b0aSDavid Howells 	mntput(cache->mnt);
168d1065b0aSDavid Howells 	cache->mnt = NULL;
169d1065b0aSDavid Howells 	dput(root);
170d1065b0aSDavid Howells error_open_root:
171d1065b0aSDavid Howells 	cachefiles_end_secure(cache, saved_cred);
172e21a2f17SBaokun Li 	put_cred(cache->cache_cred);
173e21a2f17SBaokun Li 	cache->cache_cred = NULL;
174d1065b0aSDavid Howells error_getsec:
175d1065b0aSDavid Howells 	fscache_relinquish_cache(cache_cookie);
176d1065b0aSDavid Howells 	cache->cache = NULL;
177d1065b0aSDavid Howells 	pr_err("Failed to register: %d\n", ret);
178d1065b0aSDavid Howells 	return ret;
179d1065b0aSDavid Howells }
180d1065b0aSDavid Howells 
181d1065b0aSDavid Howells /*
18280f94f29SDavid Howells  * See if we have space for a number of pages and/or a number of files in the
18380f94f29SDavid Howells  * cache
18480f94f29SDavid Howells  */
cachefiles_has_space(struct cachefiles_cache * cache,unsigned fnr,unsigned bnr,enum cachefiles_has_space_for reason)18580f94f29SDavid Howells int cachefiles_has_space(struct cachefiles_cache *cache,
1863929eca7SDavid Howells 			 unsigned fnr, unsigned bnr,
1873929eca7SDavid Howells 			 enum cachefiles_has_space_for reason)
18880f94f29SDavid Howells {
18980f94f29SDavid Howells 	struct kstatfs stats;
19080f94f29SDavid Howells 	u64 b_avail, b_writing;
19180f94f29SDavid Howells 	int ret;
19280f94f29SDavid Howells 
19380f94f29SDavid Howells 	struct path path = {
19480f94f29SDavid Howells 		.mnt	= cache->mnt,
19580f94f29SDavid Howells 		.dentry	= cache->mnt->mnt_root,
19680f94f29SDavid Howells 	};
19780f94f29SDavid Howells 
19880f94f29SDavid Howells 	//_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
19980f94f29SDavid Howells 	//       (unsigned long long) cache->frun,
20080f94f29SDavid Howells 	//       (unsigned long long) cache->fcull,
20180f94f29SDavid Howells 	//       (unsigned long long) cache->fstop,
20280f94f29SDavid Howells 	//       (unsigned long long) cache->brun,
20380f94f29SDavid Howells 	//       (unsigned long long) cache->bcull,
20480f94f29SDavid Howells 	//       (unsigned long long) cache->bstop,
20580f94f29SDavid Howells 	//       fnr, bnr);
20680f94f29SDavid Howells 
20780f94f29SDavid Howells 	/* find out how many pages of blockdev are available */
20880f94f29SDavid Howells 	memset(&stats, 0, sizeof(stats));
20980f94f29SDavid Howells 
21080f94f29SDavid Howells 	ret = vfs_statfs(&path, &stats);
21180f94f29SDavid Howells 	if (ret < 0) {
21280f94f29SDavid Howells 		trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
21380f94f29SDavid Howells 					   cachefiles_trace_statfs_error);
21480f94f29SDavid Howells 		if (ret == -EIO)
21580f94f29SDavid Howells 			cachefiles_io_error(cache, "statfs failed");
21680f94f29SDavid Howells 		_leave(" = %d", ret);
21780f94f29SDavid Howells 		return ret;
21880f94f29SDavid Howells 	}
21980f94f29SDavid Howells 
2205638b067SDavid Howells 	b_avail = stats.f_bavail;
22180f94f29SDavid Howells 	b_writing = atomic_long_read(&cache->b_writing);
22280f94f29SDavid Howells 	if (b_avail > b_writing)
22380f94f29SDavid Howells 		b_avail -= b_writing;
22480f94f29SDavid Howells 	else
22580f94f29SDavid Howells 		b_avail = 0;
22680f94f29SDavid Howells 
22780f94f29SDavid Howells 	//_debug("avail %llu,%llu",
22880f94f29SDavid Howells 	//       (unsigned long long)stats.f_ffree,
22980f94f29SDavid Howells 	//       (unsigned long long)b_avail);
23080f94f29SDavid Howells 
23180f94f29SDavid Howells 	/* see if there is sufficient space */
23280f94f29SDavid Howells 	if (stats.f_ffree > fnr)
23380f94f29SDavid Howells 		stats.f_ffree -= fnr;
23480f94f29SDavid Howells 	else
23580f94f29SDavid Howells 		stats.f_ffree = 0;
23680f94f29SDavid Howells 
23780f94f29SDavid Howells 	if (b_avail > bnr)
23880f94f29SDavid Howells 		b_avail -= bnr;
23980f94f29SDavid Howells 	else
24080f94f29SDavid Howells 		b_avail = 0;
24180f94f29SDavid Howells 
24280f94f29SDavid Howells 	ret = -ENOBUFS;
24380f94f29SDavid Howells 	if (stats.f_ffree < cache->fstop ||
24480f94f29SDavid Howells 	    b_avail < cache->bstop)
2453929eca7SDavid Howells 		goto stop_and_begin_cull;
24680f94f29SDavid Howells 
24780f94f29SDavid Howells 	ret = 0;
24880f94f29SDavid Howells 	if (stats.f_ffree < cache->fcull ||
24980f94f29SDavid Howells 	    b_avail < cache->bcull)
25080f94f29SDavid Howells 		goto begin_cull;
25180f94f29SDavid Howells 
25280f94f29SDavid Howells 	if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
25380f94f29SDavid Howells 	    stats.f_ffree >= cache->frun &&
25480f94f29SDavid Howells 	    b_avail >= cache->brun &&
25580f94f29SDavid Howells 	    test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
25680f94f29SDavid Howells 	    ) {
25780f94f29SDavid Howells 		_debug("cease culling");
25880f94f29SDavid Howells 		cachefiles_state_changed(cache);
25980f94f29SDavid Howells 	}
26080f94f29SDavid Howells 
26180f94f29SDavid Howells 	//_leave(" = 0");
26280f94f29SDavid Howells 	return 0;
26380f94f29SDavid Howells 
2643929eca7SDavid Howells stop_and_begin_cull:
2653929eca7SDavid Howells 	switch (reason) {
2663929eca7SDavid Howells 	case cachefiles_has_space_for_write:
2673929eca7SDavid Howells 		fscache_count_no_write_space();
2683929eca7SDavid Howells 		break;
2693929eca7SDavid Howells 	case cachefiles_has_space_for_create:
2703929eca7SDavid Howells 		fscache_count_no_create_space();
2713929eca7SDavid Howells 		break;
2723929eca7SDavid Howells 	default:
2733929eca7SDavid Howells 		break;
2743929eca7SDavid Howells 	}
27580f94f29SDavid Howells begin_cull:
27680f94f29SDavid Howells 	if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
27780f94f29SDavid Howells 		_debug("### CULL CACHE ###");
27880f94f29SDavid Howells 		cachefiles_state_changed(cache);
27980f94f29SDavid Howells 	}
28080f94f29SDavid Howells 
28180f94f29SDavid Howells 	_leave(" = %d", ret);
28280f94f29SDavid Howells 	return ret;
28380f94f29SDavid Howells }
284d1065b0aSDavid Howells 
285d1065b0aSDavid Howells /*
2861f08c925SDavid Howells  * Mark all the objects as being out of service and queue them all for cleanup.
2871f08c925SDavid Howells  */
cachefiles_withdraw_objects(struct cachefiles_cache * cache)2881f08c925SDavid Howells static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
2891f08c925SDavid Howells {
2901f08c925SDavid Howells 	struct cachefiles_object *object;
2911f08c925SDavid Howells 	unsigned int count = 0;
2921f08c925SDavid Howells 
2931f08c925SDavid Howells 	_enter("");
2941f08c925SDavid Howells 
2951f08c925SDavid Howells 	spin_lock(&cache->object_list_lock);
2961f08c925SDavid Howells 
2971f08c925SDavid Howells 	while (!list_empty(&cache->object_list)) {
2981f08c925SDavid Howells 		object = list_first_entry(&cache->object_list,
2991f08c925SDavid Howells 					  struct cachefiles_object, cache_link);
3001f08c925SDavid Howells 		cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
3011f08c925SDavid Howells 		list_del_init(&object->cache_link);
3021f08c925SDavid Howells 		fscache_withdraw_cookie(object->cookie);
3031f08c925SDavid Howells 		count++;
3041f08c925SDavid Howells 		if ((count & 63) == 0) {
3051f08c925SDavid Howells 			spin_unlock(&cache->object_list_lock);
3061f08c925SDavid Howells 			cond_resched();
3071f08c925SDavid Howells 			spin_lock(&cache->object_list_lock);
3081f08c925SDavid Howells 		}
3091f08c925SDavid Howells 	}
3101f08c925SDavid Howells 
3111f08c925SDavid Howells 	spin_unlock(&cache->object_list_lock);
3121f08c925SDavid Howells 	_leave(" [%u objs]", count);
3131f08c925SDavid Howells }
3141f08c925SDavid Howells 
3151f08c925SDavid Howells /*
316*5d8f8057SBaokun Li  * Withdraw fscache volumes.
317*5d8f8057SBaokun Li  */
cachefiles_withdraw_fscache_volumes(struct cachefiles_cache * cache)318*5d8f8057SBaokun Li static void cachefiles_withdraw_fscache_volumes(struct cachefiles_cache *cache)
319*5d8f8057SBaokun Li {
320*5d8f8057SBaokun Li 	struct list_head *cur;
321*5d8f8057SBaokun Li 	struct cachefiles_volume *volume;
322*5d8f8057SBaokun Li 	struct fscache_volume *vcookie;
323*5d8f8057SBaokun Li 
324*5d8f8057SBaokun Li 	_enter("");
325*5d8f8057SBaokun Li retry:
326*5d8f8057SBaokun Li 	spin_lock(&cache->object_list_lock);
327*5d8f8057SBaokun Li 	list_for_each(cur, &cache->volumes) {
328*5d8f8057SBaokun Li 		volume = list_entry(cur, struct cachefiles_volume, cache_link);
329*5d8f8057SBaokun Li 
330*5d8f8057SBaokun Li 		if (atomic_read(&volume->vcookie->n_accesses) == 0)
331*5d8f8057SBaokun Li 			continue;
332*5d8f8057SBaokun Li 
333*5d8f8057SBaokun Li 		vcookie = fscache_try_get_volume(volume->vcookie,
334*5d8f8057SBaokun Li 						 fscache_volume_get_withdraw);
335*5d8f8057SBaokun Li 		if (vcookie) {
336*5d8f8057SBaokun Li 			spin_unlock(&cache->object_list_lock);
337*5d8f8057SBaokun Li 			fscache_withdraw_volume(vcookie);
338*5d8f8057SBaokun Li 			fscache_put_volume(vcookie, fscache_volume_put_withdraw);
339*5d8f8057SBaokun Li 			goto retry;
340*5d8f8057SBaokun Li 		}
341*5d8f8057SBaokun Li 	}
342*5d8f8057SBaokun Li 	spin_unlock(&cache->object_list_lock);
343*5d8f8057SBaokun Li 
344*5d8f8057SBaokun Li 	_leave("");
345*5d8f8057SBaokun Li }
346*5d8f8057SBaokun Li 
347*5d8f8057SBaokun Li /*
348*5d8f8057SBaokun Li  * Withdraw cachefiles volumes.
349fe2140e2SDavid Howells  */
cachefiles_withdraw_volumes(struct cachefiles_cache * cache)350fe2140e2SDavid Howells static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
351fe2140e2SDavid Howells {
352fe2140e2SDavid Howells 	_enter("");
353fe2140e2SDavid Howells 
354fe2140e2SDavid Howells 	for (;;) {
355522018a0SBaokun Li 		struct fscache_volume *vcookie = NULL;
356fe2140e2SDavid Howells 		struct cachefiles_volume *volume = NULL;
357fe2140e2SDavid Howells 
358fe2140e2SDavid Howells 		spin_lock(&cache->object_list_lock);
359fe2140e2SDavid Howells 		if (!list_empty(&cache->volumes)) {
360fe2140e2SDavid Howells 			volume = list_first_entry(&cache->volumes,
361fe2140e2SDavid Howells 						  struct cachefiles_volume, cache_link);
362522018a0SBaokun Li 			vcookie = fscache_try_get_volume(volume->vcookie,
363522018a0SBaokun Li 							 fscache_volume_get_withdraw);
364522018a0SBaokun Li 			if (!vcookie) {
365522018a0SBaokun Li 				spin_unlock(&cache->object_list_lock);
366522018a0SBaokun Li 				cpu_relax();
367522018a0SBaokun Li 				continue;
368522018a0SBaokun Li 			}
369fe2140e2SDavid Howells 			list_del_init(&volume->cache_link);
370fe2140e2SDavid Howells 		}
371fe2140e2SDavid Howells 		spin_unlock(&cache->object_list_lock);
372fe2140e2SDavid Howells 		if (!volume)
373fe2140e2SDavid Howells 			break;
374fe2140e2SDavid Howells 
375fe2140e2SDavid Howells 		cachefiles_withdraw_volume(volume);
376522018a0SBaokun Li 		fscache_put_volume(vcookie, fscache_volume_put_withdraw);
377fe2140e2SDavid Howells 	}
378fe2140e2SDavid Howells 
379fe2140e2SDavid Howells 	_leave("");
380fe2140e2SDavid Howells }
381fe2140e2SDavid Howells 
382fe2140e2SDavid Howells /*
383d1065b0aSDavid Howells  * Sync a cache to backing disk.
384d1065b0aSDavid Howells  */
cachefiles_sync_cache(struct cachefiles_cache * cache)385d1065b0aSDavid Howells static void cachefiles_sync_cache(struct cachefiles_cache *cache)
386d1065b0aSDavid Howells {
387d1065b0aSDavid Howells 	const struct cred *saved_cred;
388d1065b0aSDavid Howells 	int ret;
389d1065b0aSDavid Howells 
390d1065b0aSDavid Howells 	_enter("%s", cache->cache->name);
391d1065b0aSDavid Howells 
392d1065b0aSDavid Howells 	/* make sure all pages pinned by operations on behalf of the netfs are
393d1065b0aSDavid Howells 	 * written to disc */
394d1065b0aSDavid Howells 	cachefiles_begin_secure(cache, &saved_cred);
395d1065b0aSDavid Howells 	down_read(&cache->mnt->mnt_sb->s_umount);
396d1065b0aSDavid Howells 	ret = sync_filesystem(cache->mnt->mnt_sb);
397d1065b0aSDavid Howells 	up_read(&cache->mnt->mnt_sb->s_umount);
398d1065b0aSDavid Howells 	cachefiles_end_secure(cache, saved_cred);
399d1065b0aSDavid Howells 
400d1065b0aSDavid Howells 	if (ret == -EIO)
401d1065b0aSDavid Howells 		cachefiles_io_error(cache,
402d1065b0aSDavid Howells 				    "Attempt to sync backing fs superblock returned error %d",
403d1065b0aSDavid Howells 				    ret);
404d1065b0aSDavid Howells }
405d1065b0aSDavid Howells 
406d1065b0aSDavid Howells /*
407d1065b0aSDavid Howells  * Withdraw cache objects.
408d1065b0aSDavid Howells  */
cachefiles_withdraw_cache(struct cachefiles_cache * cache)409d1065b0aSDavid Howells void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
410d1065b0aSDavid Howells {
411d1065b0aSDavid Howells 	struct fscache_cache *fscache = cache->cache;
412d1065b0aSDavid Howells 
413d1065b0aSDavid Howells 	pr_info("File cache on %s unregistering\n", fscache->name);
414d1065b0aSDavid Howells 
415d1065b0aSDavid Howells 	fscache_withdraw_cache(fscache);
416*5d8f8057SBaokun Li 	cachefiles_withdraw_fscache_volumes(cache);
417d1065b0aSDavid Howells 
418d1065b0aSDavid Howells 	/* we now have to destroy all the active objects pertaining to this
419d1065b0aSDavid Howells 	 * cache - which we do by passing them off to thread pool to be
420d1065b0aSDavid Howells 	 * disposed of */
4211f08c925SDavid Howells 	cachefiles_withdraw_objects(cache);
422d1065b0aSDavid Howells 	fscache_wait_for_objects(fscache);
423d1065b0aSDavid Howells 
424fe2140e2SDavid Howells 	cachefiles_withdraw_volumes(cache);
425d1065b0aSDavid Howells 	cachefiles_sync_cache(cache);
426d1065b0aSDavid Howells 	cache->cache = NULL;
427d1065b0aSDavid Howells 	fscache_relinquish_cache(fscache);
428d1065b0aSDavid Howells }
429