xref: /linux/fs/ceph/cache.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Ceph cache definitions.
4  *
5  *  Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
6  *  Written by Milosz Tanski (milosz@adfin.com)
7  */
8 
9 #include <linux/ceph/ceph_debug.h>
10 
11 #include <linux/fs_context.h>
12 #include "super.h"
13 #include "cache.h"
14 
15 void ceph_fscache_register_inode_cookie(struct inode *inode)
16 {
17 	struct ceph_inode_info *ci = ceph_inode(inode);
18 	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
19 
20 	/* No caching for filesystem? */
21 	if (!fsc->fscache)
22 		return;
23 
24 	/* Regular files only */
25 	if (!S_ISREG(inode->i_mode))
26 		return;
27 
28 	/* Only new inodes! */
29 	if (!(inode->i_state & I_NEW))
30 		return;
31 
32 	WARN_ON_ONCE(ci->netfs.cache);
33 
34 	ci->netfs.cache =
35 		fscache_acquire_cookie(fsc->fscache, 0,
36 				       &ci->i_vino, sizeof(ci->i_vino),
37 				       &ci->i_version, sizeof(ci->i_version),
38 				       i_size_read(inode));
39 }
40 
41 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info *ci)
42 {
43 	fscache_relinquish_cookie(ceph_fscache_cookie(ci), false);
44 }
45 
46 void ceph_fscache_use_cookie(struct inode *inode, bool will_modify)
47 {
48 	struct ceph_inode_info *ci = ceph_inode(inode);
49 
50 	fscache_use_cookie(ceph_fscache_cookie(ci), will_modify);
51 }
52 
53 void ceph_fscache_unuse_cookie(struct inode *inode, bool update)
54 {
55 	struct ceph_inode_info *ci = ceph_inode(inode);
56 
57 	if (update) {
58 		loff_t i_size = i_size_read(inode);
59 
60 		fscache_unuse_cookie(ceph_fscache_cookie(ci),
61 				     &ci->i_version, &i_size);
62 	} else {
63 		fscache_unuse_cookie(ceph_fscache_cookie(ci), NULL, NULL);
64 	}
65 }
66 
67 void ceph_fscache_update(struct inode *inode)
68 {
69 	struct ceph_inode_info *ci = ceph_inode(inode);
70 	loff_t i_size = i_size_read(inode);
71 
72 	fscache_update_cookie(ceph_fscache_cookie(ci), &ci->i_version, &i_size);
73 }
74 
75 void ceph_fscache_invalidate(struct inode *inode, bool dio_write)
76 {
77 	struct ceph_inode_info *ci = ceph_inode(inode);
78 
79 	fscache_invalidate(ceph_fscache_cookie(ci),
80 			   &ci->i_version, i_size_read(inode),
81 			   dio_write ? FSCACHE_INVAL_DIO_WRITE : 0);
82 }
83 
84 int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
85 {
86 	const struct ceph_fsid *fsid = &fsc->client->fsid;
87 	const char *fscache_uniq = fsc->mount_options->fscache_uniq;
88 	size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
89 	char *name;
90 	int err = 0;
91 
92 	name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "",
93 			 uniq_len ? fscache_uniq : "");
94 	if (!name)
95 		return -ENOMEM;
96 
97 	fsc->fscache = fscache_acquire_volume(name, NULL, NULL, 0);
98 	if (IS_ERR_OR_NULL(fsc->fscache)) {
99 		errorfc(fc, "Unable to register fscache cookie for %s", name);
100 		err = fsc->fscache ? PTR_ERR(fsc->fscache) : -EOPNOTSUPP;
101 		fsc->fscache = NULL;
102 	}
103 	kfree(name);
104 	return err;
105 }
106 
107 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
108 {
109 	fscache_relinquish_volume(fsc->fscache, NULL, false);
110 }
111