1 // SPDX-License-Identifier: GPL-2.0-only
3 * Ceph cache definitions.
5 * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
12 struct ceph_aux_inode {
18 struct fscache_netfs ceph_cache_netfs = {
23 static DEFINE_MUTEX(ceph_fscache_lock);
24 static LIST_HEAD(ceph_fscache_list);
26 struct ceph_fscache_entry {
27 struct list_head list;
28 struct fscache_cookie *fscache;
30 /* The following members must be last */
31 struct ceph_fsid fsid;
35 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
37 .type = FSCACHE_COOKIE_TYPE_INDEX,
40 int __init ceph_fscache_register(void)
42 return fscache_register_netfs(&ceph_cache_netfs);
45 void ceph_fscache_unregister(void)
47 fscache_unregister_netfs(&ceph_cache_netfs);
50 int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
52 const struct ceph_fsid *fsid = &fsc->client->fsid;
53 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
54 size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
55 struct ceph_fscache_entry *ent;
58 mutex_lock(&ceph_fscache_lock);
59 list_for_each_entry(ent, &ceph_fscache_list, list) {
60 if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
62 if (ent->uniq_len != uniq_len)
64 if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
67 pr_err("fscache cookie already registered for fsid %pU\n", fsid);
68 pr_err(" use fsc=%%s mount option to specify a uniquifier\n");
73 ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
79 memcpy(&ent->fsid, fsid, sizeof(*fsid));
81 memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
82 ent->uniq_len = uniq_len;
85 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
86 &ceph_fscache_fsid_object_def,
87 &ent->fsid, sizeof(ent->fsid) + uniq_len,
92 ent->fscache = fsc->fscache;
93 list_add_tail(&ent->list, &ceph_fscache_list);
96 pr_err("unable to register fscache cookie for fsid %pU\n",
98 /* all other fs ignore this error */
101 mutex_unlock(&ceph_fscache_lock);
105 static enum fscache_checkaux ceph_fscache_inode_check_aux(
106 void *cookie_netfs_data, const void *data, uint16_t dlen,
109 struct ceph_aux_inode aux;
110 struct ceph_inode_info* ci = cookie_netfs_data;
111 struct inode* inode = &ci->vfs_inode;
113 if (dlen != sizeof(aux) ||
114 i_size_read(inode) != object_size)
115 return FSCACHE_CHECKAUX_OBSOLETE;
117 memset(&aux, 0, sizeof(aux));
118 aux.version = ci->i_version;
119 aux.mtime_sec = inode->i_mtime.tv_sec;
120 aux.mtime_nsec = inode->i_mtime.tv_nsec;
122 if (memcmp(data, &aux, sizeof(aux)) != 0)
123 return FSCACHE_CHECKAUX_OBSOLETE;
125 dout("ceph inode 0x%p cached okay\n", ci);
126 return FSCACHE_CHECKAUX_OKAY;
129 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
130 .name = "CEPH.inode",
131 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
132 .check_aux = ceph_fscache_inode_check_aux,
135 void ceph_fscache_register_inode_cookie(struct inode *inode)
137 struct ceph_inode_info *ci = ceph_inode(inode);
138 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
139 struct ceph_aux_inode aux;
141 /* No caching for filesystem */
145 /* Only cache for regular files that are read only */
146 if (!S_ISREG(inode->i_mode))
149 inode_lock_nested(inode, I_MUTEX_CHILD);
151 memset(&aux, 0, sizeof(aux));
152 aux.version = ci->i_version;
153 aux.mtime_sec = inode->i_mtime.tv_sec;
154 aux.mtime_nsec = inode->i_mtime.tv_nsec;
155 ci->fscache = fscache_acquire_cookie(fsc->fscache,
156 &ceph_fscache_inode_object_def,
157 &ci->i_vino, sizeof(ci->i_vino),
159 ci, i_size_read(inode), false);
164 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
166 struct fscache_cookie* cookie;
168 if ((cookie = ci->fscache) == NULL)
173 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
174 fscache_relinquish_cookie(cookie, &ci->i_vino, false);
177 static bool ceph_fscache_can_enable(void *data)
179 struct inode *inode = data;
180 return !inode_is_open_for_write(inode);
183 void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
185 struct ceph_inode_info *ci = ceph_inode(inode);
187 if (!fscache_cookie_valid(ci->fscache))
190 if (inode_is_open_for_write(inode)) {
191 dout("fscache_file_set_cookie %p %p disabling cache\n",
193 fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
194 fscache_uncache_all_inode_pages(ci->fscache, inode);
196 fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode),
197 ceph_fscache_can_enable, inode);
198 if (fscache_cookie_enabled(ci->fscache)) {
199 dout("fscache_file_set_cookie %p %p enabling cache\n",
205 static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
208 SetPageUptodate(page);
213 static inline bool cache_valid(struct ceph_inode_info *ci)
215 return ci->i_fscache_gen == ci->i_rdcache_gen;
219 /* Atempt to read from the fscache,
221 * This function is called from the readpage_nounlock context. DO NOT attempt to
222 * unlock the page here (or in the callback).
224 int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
226 struct ceph_inode_info *ci = ceph_inode(inode);
229 if (!cache_valid(ci))
232 ret = fscache_read_or_alloc_page(ci->fscache, page,
233 ceph_readpage_from_fscache_complete, NULL,
237 case 0: /* Page found */
238 dout("page read submitted\n");
240 case -ENOBUFS: /* Pages were not found, and can't be */
241 case -ENODATA: /* Pages were not found */
242 dout("page/inode not in cache\n");
245 dout("%s: unknown error ret = %i\n", __func__, ret);
250 int ceph_readpages_from_fscache(struct inode *inode,
251 struct address_space *mapping,
252 struct list_head *pages,
255 struct ceph_inode_info *ci = ceph_inode(inode);
258 if (!cache_valid(ci))
261 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
262 ceph_readpage_from_fscache_complete,
263 NULL, mapping_gfp_mask(mapping));
266 case 0: /* All pages found */
267 dout("all-page read submitted\n");
269 case -ENOBUFS: /* Some pages were not found, and can't be */
270 case -ENODATA: /* some pages were not found */
271 dout("page/inode not in cache\n");
274 dout("%s: unknown error ret = %i\n", __func__, ret);
279 void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
281 struct ceph_inode_info *ci = ceph_inode(inode);
284 if (!PageFsCache(page))
287 if (!cache_valid(ci))
290 ret = fscache_write_page(ci->fscache, page, i_size_read(inode),
293 fscache_uncache_page(ci->fscache, page);
296 void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
298 struct ceph_inode_info *ci = ceph_inode(inode);
300 if (!PageFsCache(page))
303 fscache_wait_on_page_write(ci->fscache, page);
304 fscache_uncache_page(ci->fscache, page);
307 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
309 if (fscache_cookie_valid(fsc->fscache)) {
310 struct ceph_fscache_entry *ent;
313 mutex_lock(&ceph_fscache_lock);
314 list_for_each_entry(ent, &ceph_fscache_list, list) {
315 if (ent->fscache == fsc->fscache) {
316 list_del(&ent->list);
322 WARN_ON_ONCE(!found);
323 mutex_unlock(&ceph_fscache_lock);
325 __fscache_relinquish_cookie(fsc->fscache, NULL, false);
331 * caller should hold CEPH_CAP_FILE_{RD,CACHE}
333 void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
338 /* resue i_truncate_mutex. There should be no pending
339 * truncate while the caller holds CEPH_CAP_FILE_RD */
340 mutex_lock(&ci->i_truncate_mutex);
341 if (!cache_valid(ci)) {
342 if (fscache_check_consistency(ci->fscache, &ci->i_vino))
343 fscache_invalidate(ci->fscache);
344 spin_lock(&ci->i_ceph_lock);
345 ci->i_fscache_gen = ci->i_rdcache_gen;
346 spin_unlock(&ci->i_ceph_lock);
348 mutex_unlock(&ci->i_truncate_mutex);