xref: /openbmc/linux/fs/ceph/cache.c (revision 985b9ee8)
11f327613SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
299ccbd22SMilosz Tanski /*
399ccbd22SMilosz Tanski  * Ceph cache definitions.
499ccbd22SMilosz Tanski  *
599ccbd22SMilosz Tanski  *  Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
699ccbd22SMilosz Tanski  *  Written by Milosz Tanski (milosz@adfin.com)
799ccbd22SMilosz Tanski  */
899ccbd22SMilosz Tanski 
948f930eaSIlya Dryomov #include <linux/ceph/ceph_debug.h>
1048f930eaSIlya Dryomov 
1182995cc6SDavid Howells #include <linux/fs_context.h>
1299ccbd22SMilosz Tanski #include "super.h"
1399ccbd22SMilosz Tanski #include "cache.h"
1499ccbd22SMilosz Tanski 
ceph_fscache_register_inode_cookie(struct inode * inode)15400e1286SJeff Layton void ceph_fscache_register_inode_cookie(struct inode *inode)
1699ccbd22SMilosz Tanski {
17400e1286SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
18985b9ee8SXiubo Li 	struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
19400e1286SJeff Layton 
20400e1286SJeff Layton 	/* No caching for filesystem? */
21400e1286SJeff Layton 	if (!fsc->fscache)
22400e1286SJeff Layton 		return;
23400e1286SJeff Layton 
24400e1286SJeff Layton 	/* Regular files only */
25400e1286SJeff Layton 	if (!S_ISREG(inode->i_mode))
26400e1286SJeff Layton 		return;
27400e1286SJeff Layton 
28400e1286SJeff Layton 	/* Only new inodes! */
29400e1286SJeff Layton 	if (!(inode->i_state & I_NEW))
30400e1286SJeff Layton 		return;
31400e1286SJeff Layton 
32874c8ca1SDavid Howells 	WARN_ON_ONCE(ci->netfs.cache);
33400e1286SJeff Layton 
34874c8ca1SDavid Howells 	ci->netfs.cache =
35bc899ee1SDavid Howells 		fscache_acquire_cookie(fsc->fscache, 0,
36400e1286SJeff Layton 				       &ci->i_vino, sizeof(ci->i_vino),
37400e1286SJeff Layton 				       &ci->i_version, sizeof(ci->i_version),
38400e1286SJeff Layton 				       i_size_read(inode));
39b4fa966fSDavid Howells 	if (ci->netfs.cache)
40b4fa966fSDavid Howells 		mapping_set_release_always(inode->i_mapping);
4199ccbd22SMilosz Tanski }
4299ccbd22SMilosz Tanski 
ceph_fscache_unregister_inode_cookie(struct ceph_inode_info * ci)43400e1286SJeff Layton void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info *ci)
4499ccbd22SMilosz Tanski {
45bc899ee1SDavid Howells 	fscache_relinquish_cookie(ceph_fscache_cookie(ci), false);
46400e1286SJeff Layton }
47400e1286SJeff Layton 
ceph_fscache_use_cookie(struct inode * inode,bool will_modify)48400e1286SJeff Layton void ceph_fscache_use_cookie(struct inode *inode, bool will_modify)
49400e1286SJeff Layton {
50400e1286SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
51400e1286SJeff Layton 
52bc899ee1SDavid Howells 	fscache_use_cookie(ceph_fscache_cookie(ci), will_modify);
53400e1286SJeff Layton }
54400e1286SJeff Layton 
ceph_fscache_unuse_cookie(struct inode * inode,bool update)55400e1286SJeff Layton void ceph_fscache_unuse_cookie(struct inode *inode, bool update)
56400e1286SJeff Layton {
57400e1286SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
58400e1286SJeff Layton 
59400e1286SJeff Layton 	if (update) {
60400e1286SJeff Layton 		loff_t i_size = i_size_read(inode);
61400e1286SJeff Layton 
62bc899ee1SDavid Howells 		fscache_unuse_cookie(ceph_fscache_cookie(ci),
63bc899ee1SDavid Howells 				     &ci->i_version, &i_size);
64400e1286SJeff Layton 	} else {
65bc899ee1SDavid Howells 		fscache_unuse_cookie(ceph_fscache_cookie(ci), NULL, NULL);
66400e1286SJeff Layton 	}
67400e1286SJeff Layton }
68400e1286SJeff Layton 
ceph_fscache_update(struct inode * inode)69400e1286SJeff Layton void ceph_fscache_update(struct inode *inode)
70400e1286SJeff Layton {
71400e1286SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
72400e1286SJeff Layton 	loff_t i_size = i_size_read(inode);
73400e1286SJeff Layton 
74bc899ee1SDavid Howells 	fscache_update_cookie(ceph_fscache_cookie(ci), &ci->i_version, &i_size);
75400e1286SJeff Layton }
76400e1286SJeff Layton 
ceph_fscache_invalidate(struct inode * inode,bool dio_write)77400e1286SJeff Layton void ceph_fscache_invalidate(struct inode *inode, bool dio_write)
78400e1286SJeff Layton {
79400e1286SJeff Layton 	struct ceph_inode_info *ci = ceph_inode(inode);
80400e1286SJeff Layton 
81bc899ee1SDavid Howells 	fscache_invalidate(ceph_fscache_cookie(ci),
82400e1286SJeff Layton 			   &ci->i_version, i_size_read(inode),
83400e1286SJeff Layton 			   dio_write ? FSCACHE_INVAL_DIO_WRITE : 0);
8499ccbd22SMilosz Tanski }
8599ccbd22SMilosz Tanski 
ceph_fscache_register_fs(struct ceph_fs_client * fsc,struct fs_context * fc)8682995cc6SDavid Howells int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
8799ccbd22SMilosz Tanski {
881d8f8360SYan, Zheng 	const struct ceph_fsid *fsid = &fsc->client->fsid;
891d8f8360SYan, Zheng 	const char *fscache_uniq = fsc->mount_options->fscache_uniq;
901d8f8360SYan, Zheng 	size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
91400e1286SJeff Layton 	char *name;
921d8f8360SYan, Zheng 	int err = 0;
931d8f8360SYan, Zheng 
94400e1286SJeff Layton 	name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "",
95400e1286SJeff Layton 			 uniq_len ? fscache_uniq : "");
96400e1286SJeff Layton 	if (!name)
97400e1286SJeff Layton 		return -ENOMEM;
981d8f8360SYan, Zheng 
99400e1286SJeff Layton 	fsc->fscache = fscache_acquire_volume(name, NULL, NULL, 0);
100400e1286SJeff Layton 	if (IS_ERR_OR_NULL(fsc->fscache)) {
101400e1286SJeff Layton 		errorfc(fc, "Unable to register fscache cookie for %s", name);
102400e1286SJeff Layton 		err = fsc->fscache ? PTR_ERR(fsc->fscache) : -EOPNOTSUPP;
103400e1286SJeff Layton 		fsc->fscache = NULL;
1041d8f8360SYan, Zheng 	}
105400e1286SJeff Layton 	kfree(name);
1061d8f8360SYan, Zheng 	return err;
10799ccbd22SMilosz Tanski }
10899ccbd22SMilosz Tanski 
ceph_fscache_unregister_fs(struct ceph_fs_client * fsc)10999ccbd22SMilosz Tanski void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
11099ccbd22SMilosz Tanski {
111400e1286SJeff Layton 	fscache_relinquish_volume(fsc->fscache, NULL, false);
11299ccbd22SMilosz Tanski }
113