blob: fee869061f05407ea24c5be226e9da9227d967d9 [file] [log] [blame]
Milosz Tanski99ccbd22013-08-21 17:29:54 -04001/*
2 * Ceph cache definitions.
3 *
4 * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
5 * Written by Milosz Tanski (milosz@adfin.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to:
18 * Free Software Foundation
19 * 51 Franklin Street, Fifth Floor
20 * Boston, MA 02111-1301 USA
21 *
22 */
23
Milosz Tanski99ccbd22013-08-21 17:29:54 -040024#include "super.h"
25#include "cache.h"
26
27struct ceph_aux_inode {
Yan, Zhengf6973c02016-05-20 16:57:29 +080028 u64 version;
Milosz Tanski99ccbd22013-08-21 17:29:54 -040029 struct timespec mtime;
30 loff_t size;
31};
32
33struct fscache_netfs ceph_cache_netfs = {
34 .name = "ceph",
35 .version = 0,
36};
37
Yan, Zheng1d8f8362017-06-27 11:57:56 +080038static DEFINE_MUTEX(ceph_fscache_lock);
39static LIST_HEAD(ceph_fscache_list);
40
41struct ceph_fscache_entry {
42 struct list_head list;
43 struct fscache_cookie *fscache;
Yan, Zheng1d8f8362017-06-27 11:57:56 +080044 size_t uniq_len;
David Howells402cb8d2018-04-04 13:41:28 +010045 /* The following members must be last */
46 struct ceph_fsid fsid;
Yan, Zheng1d8f8362017-06-27 11:57:56 +080047 char uniquifier[0];
48};
49
Milosz Tanski99ccbd22013-08-21 17:29:54 -040050static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
51 .name = "CEPH.fsid",
52 .type = FSCACHE_COOKIE_TYPE_INDEX,
Milosz Tanski99ccbd22013-08-21 17:29:54 -040053};
54
Milosz Tanski971f0bd2013-09-06 15:13:18 +000055int ceph_fscache_register(void)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040056{
57 return fscache_register_netfs(&ceph_cache_netfs);
58}
59
Milosz Tanski971f0bd2013-09-06 15:13:18 +000060void ceph_fscache_unregister(void)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040061{
62 fscache_unregister_netfs(&ceph_cache_netfs);
63}
64
65int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
66{
Yan, Zheng1d8f8362017-06-27 11:57:56 +080067 const struct ceph_fsid *fsid = &fsc->client->fsid;
68 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
69 size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
70 struct ceph_fscache_entry *ent;
71 int err = 0;
72
73 mutex_lock(&ceph_fscache_lock);
74 list_for_each_entry(ent, &ceph_fscache_list, list) {
75 if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
76 continue;
77 if (ent->uniq_len != uniq_len)
78 continue;
79 if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
80 continue;
81
82 pr_err("fscache cookie already registered for fsid %pU\n", fsid);
83 pr_err(" use fsc=%%s mount option to specify a uniquifier\n");
84 err = -EBUSY;
85 goto out_unlock;
86 }
87
88 ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
89 if (!ent) {
90 err = -ENOMEM;
91 goto out_unlock;
92 }
93
David Howells402cb8d2018-04-04 13:41:28 +010094 memcpy(&ent->fsid, fsid, sizeof(*fsid));
95 if (uniq_len > 0) {
96 memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
97 ent->uniq_len = uniq_len;
98 }
99
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400100 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
101 &ceph_fscache_fsid_object_def,
David Howells402cb8d2018-04-04 13:41:28 +0100102 &ent->fsid, sizeof(ent->fsid) + uniq_len,
103 NULL, 0,
David Howells94d30ae2013-09-21 00:09:31 +0100104 fsc, true);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400105
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800106 if (fsc->fscache) {
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800107 ent->fscache = fsc->fscache;
108 list_add_tail(&ent->list, &ceph_fscache_list);
109 } else {
110 kfree(ent);
111 pr_err("unable to register fscache cookie for fsid %pU\n",
112 fsid);
113 /* all other fs ignore this error */
114 }
115out_unlock:
116 mutex_unlock(&ceph_fscache_lock);
117 return err;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400118}
119
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400120static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
121 uint64_t *size)
122{
123 const struct ceph_inode_info* ci = cookie_netfs_data;
Yan, Zheng99c88e62015-12-30 11:32:46 +0800124 *size = i_size_read(&ci->vfs_inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400125}
126
127static enum fscache_checkaux ceph_fscache_inode_check_aux(
128 void *cookie_netfs_data, const void *data, uint16_t dlen)
129{
130 struct ceph_aux_inode aux;
131 struct ceph_inode_info* ci = cookie_netfs_data;
132 struct inode* inode = &ci->vfs_inode;
133
134 if (dlen != sizeof(aux))
135 return FSCACHE_CHECKAUX_OBSOLETE;
136
137 memset(&aux, 0, sizeof(aux));
Yan, Zhengf6973c02016-05-20 16:57:29 +0800138 aux.version = ci->i_version;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400139 aux.mtime = inode->i_mtime;
Yan, Zheng99c88e62015-12-30 11:32:46 +0800140 aux.size = i_size_read(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400141
142 if (memcmp(data, &aux, sizeof(aux)) != 0)
143 return FSCACHE_CHECKAUX_OBSOLETE;
144
145 dout("ceph inode 0x%p cached okay", ci);
146 return FSCACHE_CHECKAUX_OKAY;
147}
148
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400149static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
150 .name = "CEPH.inode",
151 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400152 .get_attr = ceph_fscache_inode_get_attr,
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400153 .check_aux = ceph_fscache_inode_check_aux,
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400154};
155
Yan, Zheng46b59b22016-05-18 15:25:03 +0800156void ceph_fscache_register_inode_cookie(struct inode *inode)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400157{
Yan, Zheng46b59b22016-05-18 15:25:03 +0800158 struct ceph_inode_info *ci = ceph_inode(inode);
159 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
David Howells402cb8d2018-04-04 13:41:28 +0100160 struct ceph_aux_inode aux;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400161
162 /* No caching for filesystem */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200163 if (!fsc->fscache)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400164 return;
165
166 /* Only cache for regular files that are read only */
Yan, Zheng46b59b22016-05-18 15:25:03 +0800167 if (!S_ISREG(inode->i_mode))
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400168 return;
169
Yan, Zheng46b59b22016-05-18 15:25:03 +0800170 inode_lock_nested(inode, I_MUTEX_CHILD);
171 if (!ci->fscache) {
David Howells402cb8d2018-04-04 13:41:28 +0100172 memset(&aux, 0, sizeof(aux));
173 aux.version = ci->i_version;
174 aux.mtime = inode->i_mtime;
175 aux.size = i_size_read(inode);
Yan, Zheng46b59b22016-05-18 15:25:03 +0800176 ci->fscache = fscache_acquire_cookie(fsc->fscache,
David Howells402cb8d2018-04-04 13:41:28 +0100177 &ceph_fscache_inode_object_def,
178 &ci->i_vino, sizeof(ci->i_vino),
179 &aux, sizeof(aux),
180 ci, false);
Yan, Zheng46b59b22016-05-18 15:25:03 +0800181 }
Al Viro59551022016-01-22 15:40:57 -0500182 inode_unlock(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400183}
184
185void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
186{
187 struct fscache_cookie* cookie;
188
189 if ((cookie = ci->fscache) == NULL)
190 return;
191
192 ci->fscache = NULL;
193
194 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
David Howells402cb8d2018-04-04 13:41:28 +0100195 fscache_relinquish_cookie(cookie, &ci->i_vino, false);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400196}
197
Yan, Zheng46b59b22016-05-18 15:25:03 +0800198static bool ceph_fscache_can_enable(void *data)
199{
200 struct inode *inode = data;
201 return !inode_is_open_for_write(inode);
202}
203
204void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
205{
206 struct ceph_inode_info *ci = ceph_inode(inode);
207
208 if (!fscache_cookie_valid(ci->fscache))
209 return;
210
211 if (inode_is_open_for_write(inode)) {
212 dout("fscache_file_set_cookie %p %p disabling cache\n",
213 inode, filp);
David Howells402cb8d2018-04-04 13:41:28 +0100214 fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
Yan, Zheng46b59b22016-05-18 15:25:03 +0800215 fscache_uncache_all_inode_pages(ci->fscache, inode);
216 } else {
David Howells402cb8d2018-04-04 13:41:28 +0100217 fscache_enable_cookie(ci->fscache, &ci->i_vino,
218 ceph_fscache_can_enable, inode);
Yan, Zheng46b59b22016-05-18 15:25:03 +0800219 if (fscache_cookie_enabled(ci->fscache)) {
Colin Ian King0fbc5362016-12-29 20:19:32 +0000220 dout("fscache_file_set_cookie %p %p enabling cache\n",
Yan, Zheng46b59b22016-05-18 15:25:03 +0800221 inode, filp);
222 }
223 }
224}
225
Yan, Zhengdd2bc472017-08-04 11:22:31 +0800226static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400227{
228 if (!error)
229 SetPageUptodate(page);
230
231 unlock_page(page);
232}
233
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400234static inline bool cache_valid(struct ceph_inode_info *ci)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400235{
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800236 return ci->i_fscache_gen == ci->i_rdcache_gen;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400237}
238
239
240/* Atempt to read from the fscache,
241 *
242 * This function is called from the readpage_nounlock context. DO NOT attempt to
243 * unlock the page here (or in the callback).
244 */
245int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
246{
247 struct ceph_inode_info *ci = ceph_inode(inode);
248 int ret;
249
250 if (!cache_valid(ci))
251 return -ENOBUFS;
252
253 ret = fscache_read_or_alloc_page(ci->fscache, page,
Yan, Zhengdd2bc472017-08-04 11:22:31 +0800254 ceph_readpage_from_fscache_complete, NULL,
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400255 GFP_KERNEL);
256
257 switch (ret) {
258 case 0: /* Page found */
259 dout("page read submitted\n");
260 return 0;
261 case -ENOBUFS: /* Pages were not found, and can't be */
262 case -ENODATA: /* Pages were not found */
263 dout("page/inode not in cache\n");
264 return ret;
265 default:
266 dout("%s: unknown error ret = %i\n", __func__, ret);
267 return ret;
268 }
269}
270
271int ceph_readpages_from_fscache(struct inode *inode,
272 struct address_space *mapping,
273 struct list_head *pages,
274 unsigned *nr_pages)
275{
276 struct ceph_inode_info *ci = ceph_inode(inode);
277 int ret;
278
279 if (!cache_valid(ci))
280 return -ENOBUFS;
281
282 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
Yan, Zhengdd2bc472017-08-04 11:22:31 +0800283 ceph_readpage_from_fscache_complete,
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400284 NULL, mapping_gfp_mask(mapping));
285
286 switch (ret) {
287 case 0: /* All pages found */
288 dout("all-page read submitted\n");
289 return 0;
290 case -ENOBUFS: /* Some pages were not found, and can't be */
291 case -ENODATA: /* some pages were not found */
292 dout("page/inode not in cache\n");
293 return ret;
294 default:
295 dout("%s: unknown error ret = %i\n", __func__, ret);
296 return ret;
297 }
298}
299
300void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
301{
302 struct ceph_inode_info *ci = ceph_inode(inode);
303 int ret;
304
Milosz Tanski9b8dd1e2013-09-03 19:11:01 -0400305 if (!PageFsCache(page))
306 return;
307
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400308 if (!cache_valid(ci))
309 return;
310
311 ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
312 if (ret)
313 fscache_uncache_page(ci->fscache, page);
314}
315
316void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
317{
318 struct ceph_inode_info *ci = ceph_inode(inode);
319
Milosz Tanskiffc79662013-09-25 11:18:14 -0400320 if (!PageFsCache(page))
321 return;
322
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400323 fscache_wait_on_page_write(ci->fscache, page);
324 fscache_uncache_page(ci->fscache, page);
325}
326
327void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
328{
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800329 if (fscache_cookie_valid(fsc->fscache)) {
330 struct ceph_fscache_entry *ent;
331 bool found = false;
332
333 mutex_lock(&ceph_fscache_lock);
334 list_for_each_entry(ent, &ceph_fscache_list, list) {
335 if (ent->fscache == fsc->fscache) {
336 list_del(&ent->list);
337 kfree(ent);
338 found = true;
339 break;
340 }
341 }
342 WARN_ON_ONCE(!found);
343 mutex_unlock(&ceph_fscache_lock);
344
David Howells402cb8d2018-04-04 13:41:28 +0100345 __fscache_relinquish_cookie(fsc->fscache, NULL, false);
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800346 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400347 fsc->fscache = NULL;
348}
349
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800350/*
351 * caller should hold CEPH_CAP_FILE_{RD,CACHE}
352 */
353void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400354{
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800355 if (cache_valid(ci))
Milosz Tanskie81568e2013-09-05 18:29:03 +0000356 return;
357
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800358 /* resue i_truncate_mutex. There should be no pending
359 * truncate while the caller holds CEPH_CAP_FILE_RD */
360 mutex_lock(&ci->i_truncate_mutex);
361 if (!cache_valid(ci)) {
David Howells402cb8d2018-04-04 13:41:28 +0100362 if (fscache_check_consistency(ci->fscache, &ci->i_vino))
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800363 fscache_invalidate(ci->fscache);
364 spin_lock(&ci->i_ceph_lock);
365 ci->i_fscache_gen = ci->i_rdcache_gen;
366 spin_unlock(&ci->i_ceph_lock);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400367 }
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800368 mutex_unlock(&ci->i_truncate_mutex);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400369}