1 // SPDX-License-Identifier: GPL-2.0-only
3 * Ceph cache definitions.
5 * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
9 #include <linux/ceph/ceph_debug.h>
11 #include <linux/fs_context.h>
15 struct fscache_netfs ceph_cache_netfs = {
20 static DEFINE_MUTEX(ceph_fscache_lock);
21 static LIST_HEAD(ceph_fscache_list);
23 struct ceph_fscache_entry {
24 struct list_head list;
25 struct fscache_cookie *fscache;
27 /* The following members must be last */
28 struct ceph_fsid fsid;
32 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
34 .type = FSCACHE_COOKIE_TYPE_INDEX,
37 int __init ceph_fscache_register(void)
39 return fscache_register_netfs(&ceph_cache_netfs);
42 void ceph_fscache_unregister(void)
44 fscache_unregister_netfs(&ceph_cache_netfs);
47 int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
49 const struct ceph_fsid *fsid = &fsc->client->fsid;
50 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
51 size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
52 struct ceph_fscache_entry *ent;
55 mutex_lock(&ceph_fscache_lock);
56 list_for_each_entry(ent, &ceph_fscache_list, list) {
57 if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
59 if (ent->uniq_len != uniq_len)
61 if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
64 errorfc(fc, "fscache cookie already registered for fsid %pU, use fsc=<uniquifier> option",
70 ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
76 memcpy(&ent->fsid, fsid, sizeof(*fsid));
78 memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
79 ent->uniq_len = uniq_len;
82 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
83 &ceph_fscache_fsid_object_def,
84 &ent->fsid, sizeof(ent->fsid) + uniq_len,
89 ent->fscache = fsc->fscache;
90 list_add_tail(&ent->list, &ceph_fscache_list);
93 errorfc(fc, "unable to register fscache cookie for fsid %pU",
95 /* all other fs ignore this error */
98 mutex_unlock(&ceph_fscache_lock);
102 static enum fscache_checkaux ceph_fscache_inode_check_aux(
103 void *cookie_netfs_data, const void *data, uint16_t dlen,
106 struct ceph_inode_info* ci = cookie_netfs_data;
107 struct inode* inode = &ci->vfs_inode;
109 if (dlen != sizeof(ci->i_version) ||
110 i_size_read(inode) != object_size)
111 return FSCACHE_CHECKAUX_OBSOLETE;
113 if (*(u64 *)data != ci->i_version)
114 return FSCACHE_CHECKAUX_OBSOLETE;
116 dout("ceph inode 0x%p cached okay\n", ci);
117 return FSCACHE_CHECKAUX_OKAY;
120 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
121 .name = "CEPH.inode",
122 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
123 .check_aux = ceph_fscache_inode_check_aux,
126 void ceph_fscache_register_inode_cookie(struct inode *inode)
128 struct ceph_inode_info *ci = ceph_inode(inode);
129 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
131 /* No caching for filesystem */
135 /* Only cache for regular files that are read only */
136 if (!S_ISREG(inode->i_mode))
139 inode_lock_nested(inode, I_MUTEX_CHILD);
141 ci->fscache = fscache_acquire_cookie(fsc->fscache,
142 &ceph_fscache_inode_object_def,
143 &ci->i_vino, sizeof(ci->i_vino),
144 &ci->i_version, sizeof(ci->i_version),
145 ci, i_size_read(inode), false);
150 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
152 struct fscache_cookie* cookie;
154 if ((cookie = ci->fscache) == NULL)
159 fscache_relinquish_cookie(cookie, &ci->i_vino, false);
162 static bool ceph_fscache_can_enable(void *data)
164 struct inode *inode = data;
165 return !inode_is_open_for_write(inode);
168 void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
170 struct ceph_inode_info *ci = ceph_inode(inode);
172 if (!fscache_cookie_valid(ci->fscache))
175 if (inode_is_open_for_write(inode)) {
176 dout("fscache_file_set_cookie %p %p disabling cache\n",
178 fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
180 fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode),
181 ceph_fscache_can_enable, inode);
182 if (fscache_cookie_enabled(ci->fscache)) {
183 dout("fscache_file_set_cookie %p %p enabling cache\n",
189 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
191 if (fscache_cookie_valid(fsc->fscache)) {
192 struct ceph_fscache_entry *ent;
195 mutex_lock(&ceph_fscache_lock);
196 list_for_each_entry(ent, &ceph_fscache_list, list) {
197 if (ent->fscache == fsc->fscache) {
198 list_del(&ent->list);
204 WARN_ON_ONCE(!found);
205 mutex_unlock(&ceph_fscache_lock);
207 __fscache_relinquish_cookie(fsc->fscache, NULL, false);