blob: fd1172823f8621b6701e91172a0f0f83c2bb3f5c [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;
44 struct ceph_fsid fsid;
45 size_t uniq_len;
46 char uniquifier[0];
47};
48
Milosz Tanski99ccbd22013-08-21 17:29:54 -040049static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
50 void *buffer, uint16_t maxbuf)
51{
52 const struct ceph_fs_client* fsc = cookie_netfs_data;
Yan, Zheng1d8f8362017-06-27 11:57:56 +080053 const char *fscache_uniq = fsc->mount_options->fscache_uniq;
54 uint16_t fsid_len, uniq_len;
Milosz Tanski99ccbd22013-08-21 17:29:54 -040055
Yan, Zheng1d8f8362017-06-27 11:57:56 +080056 fsid_len = sizeof(fsc->client->fsid);
57 uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
58 if (fsid_len + uniq_len > maxbuf)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040059 return 0;
60
Yan, Zheng1d8f8362017-06-27 11:57:56 +080061 memcpy(buffer, &fsc->client->fsid, fsid_len);
62 if (uniq_len)
63 memcpy(buffer + fsid_len, fscache_uniq, uniq_len);
64
65 return fsid_len + uniq_len;
Milosz Tanski99ccbd22013-08-21 17:29:54 -040066}
67
68static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
69 .name = "CEPH.fsid",
70 .type = FSCACHE_COOKIE_TYPE_INDEX,
71 .get_key = ceph_fscache_session_get_key,
72};
73
Milosz Tanski971f0bd2013-09-06 15:13:18 +000074int ceph_fscache_register(void)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040075{
76 return fscache_register_netfs(&ceph_cache_netfs);
77}
78
Milosz Tanski971f0bd2013-09-06 15:13:18 +000079void ceph_fscache_unregister(void)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040080{
81 fscache_unregister_netfs(&ceph_cache_netfs);
82}
83
84int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
85{
Yan, Zheng1d8f8362017-06-27 11:57:56 +080086 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 struct ceph_fscache_entry *ent;
90 int err = 0;
91
92 mutex_lock(&ceph_fscache_lock);
93 list_for_each_entry(ent, &ceph_fscache_list, list) {
94 if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
95 continue;
96 if (ent->uniq_len != uniq_len)
97 continue;
98 if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
99 continue;
100
101 pr_err("fscache cookie already registered for fsid %pU\n", fsid);
102 pr_err(" use fsc=%%s mount option to specify a uniquifier\n");
103 err = -EBUSY;
104 goto out_unlock;
105 }
106
107 ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
108 if (!ent) {
109 err = -ENOMEM;
110 goto out_unlock;
111 }
112
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400113 fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
114 &ceph_fscache_fsid_object_def,
David Howells94d30ae2013-09-21 00:09:31 +0100115 fsc, true);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400116
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800117 if (fsc->fscache) {
118 memcpy(&ent->fsid, fsid, sizeof(*fsid));
119 if (uniq_len > 0) {
120 memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
121 ent->uniq_len = uniq_len;
122 }
123 ent->fscache = fsc->fscache;
124 list_add_tail(&ent->list, &ceph_fscache_list);
125 } else {
126 kfree(ent);
127 pr_err("unable to register fscache cookie for fsid %pU\n",
128 fsid);
129 /* all other fs ignore this error */
130 }
131out_unlock:
132 mutex_unlock(&ceph_fscache_lock);
133 return err;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400134}
135
136static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
137 void *buffer, uint16_t maxbuf)
138{
139 const struct ceph_inode_info* ci = cookie_netfs_data;
140 uint16_t klen;
141
Geliang Tang1291fb92015-09-30 11:41:05 +0800142 /* use ceph virtual inode (id + snapshot) */
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400143 klen = sizeof(ci->i_vino);
144 if (klen > maxbuf)
145 return 0;
146
147 memcpy(buffer, &ci->i_vino, klen);
148 return klen;
149}
150
151static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
152 void *buffer, uint16_t bufmax)
153{
154 struct ceph_aux_inode aux;
155 const struct ceph_inode_info* ci = cookie_netfs_data;
156 const struct inode* inode = &ci->vfs_inode;
157
158 memset(&aux, 0, sizeof(aux));
Yan, Zhengf6973c02016-05-20 16:57:29 +0800159 aux.version = ci->i_version;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400160 aux.mtime = inode->i_mtime;
Yan, Zheng99c88e62015-12-30 11:32:46 +0800161 aux.size = i_size_read(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400162
163 memcpy(buffer, &aux, sizeof(aux));
164
165 return sizeof(aux);
166}
167
168static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
169 uint64_t *size)
170{
171 const struct ceph_inode_info* ci = cookie_netfs_data;
Yan, Zheng99c88e62015-12-30 11:32:46 +0800172 *size = i_size_read(&ci->vfs_inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400173}
174
175static enum fscache_checkaux ceph_fscache_inode_check_aux(
176 void *cookie_netfs_data, const void *data, uint16_t dlen)
177{
178 struct ceph_aux_inode aux;
179 struct ceph_inode_info* ci = cookie_netfs_data;
180 struct inode* inode = &ci->vfs_inode;
181
182 if (dlen != sizeof(aux))
183 return FSCACHE_CHECKAUX_OBSOLETE;
184
185 memset(&aux, 0, sizeof(aux));
Yan, Zhengf6973c02016-05-20 16:57:29 +0800186 aux.version = ci->i_version;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400187 aux.mtime = inode->i_mtime;
Yan, Zheng99c88e62015-12-30 11:32:46 +0800188 aux.size = i_size_read(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400189
190 if (memcmp(data, &aux, sizeof(aux)) != 0)
191 return FSCACHE_CHECKAUX_OBSOLETE;
192
193 dout("ceph inode 0x%p cached okay", ci);
194 return FSCACHE_CHECKAUX_OKAY;
195}
196
197static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
198{
199 struct ceph_inode_info* ci = cookie_netfs_data;
200 struct pagevec pvec;
201 pgoff_t first;
202 int loop, nr_pages;
203
204 pagevec_init(&pvec, 0);
205 first = 0;
206
207 dout("ceph inode 0x%p now uncached", ci);
208
209 while (1) {
210 nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
211 PAGEVEC_SIZE - pagevec_count(&pvec));
212
213 if (!nr_pages)
214 break;
215
216 for (loop = 0; loop < nr_pages; loop++)
217 ClearPageFsCache(pvec.pages[loop]);
218
219 first = pvec.pages[nr_pages - 1]->index + 1;
220
221 pvec.nr = nr_pages;
222 pagevec_release(&pvec);
223 cond_resched();
224 }
225}
226
227static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
228 .name = "CEPH.inode",
229 .type = FSCACHE_COOKIE_TYPE_DATAFILE,
230 .get_key = ceph_fscache_inode_get_key,
231 .get_attr = ceph_fscache_inode_get_attr,
232 .get_aux = ceph_fscache_inode_get_aux,
233 .check_aux = ceph_fscache_inode_check_aux,
234 .now_uncached = ceph_fscache_inode_now_uncached,
235};
236
Yan, Zheng46b59b22016-05-18 15:25:03 +0800237void ceph_fscache_register_inode_cookie(struct inode *inode)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400238{
Yan, Zheng46b59b22016-05-18 15:25:03 +0800239 struct ceph_inode_info *ci = ceph_inode(inode);
240 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400241
242 /* No caching for filesystem */
243 if (fsc->fscache == NULL)
244 return;
245
246 /* Only cache for regular files that are read only */
Yan, Zheng46b59b22016-05-18 15:25:03 +0800247 if (!S_ISREG(inode->i_mode))
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400248 return;
249
Yan, Zheng46b59b22016-05-18 15:25:03 +0800250 inode_lock_nested(inode, I_MUTEX_CHILD);
251 if (!ci->fscache) {
252 ci->fscache = fscache_acquire_cookie(fsc->fscache,
253 &ceph_fscache_inode_object_def,
254 ci, false);
255 }
Al Viro59551022016-01-22 15:40:57 -0500256 inode_unlock(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400257}
258
259void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
260{
261 struct fscache_cookie* cookie;
262
263 if ((cookie = ci->fscache) == NULL)
264 return;
265
266 ci->fscache = NULL;
267
268 fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
269 fscache_relinquish_cookie(cookie, 0);
270}
271
Yan, Zheng46b59b22016-05-18 15:25:03 +0800272static bool ceph_fscache_can_enable(void *data)
273{
274 struct inode *inode = data;
275 return !inode_is_open_for_write(inode);
276}
277
278void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
279{
280 struct ceph_inode_info *ci = ceph_inode(inode);
281
282 if (!fscache_cookie_valid(ci->fscache))
283 return;
284
285 if (inode_is_open_for_write(inode)) {
286 dout("fscache_file_set_cookie %p %p disabling cache\n",
287 inode, filp);
288 fscache_disable_cookie(ci->fscache, false);
289 fscache_uncache_all_inode_pages(ci->fscache, inode);
290 } else {
291 fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable,
292 inode);
293 if (fscache_cookie_enabled(ci->fscache)) {
Colin Ian King0fbc5362016-12-29 20:19:32 +0000294 dout("fscache_file_set_cookie %p %p enabling cache\n",
Yan, Zheng46b59b22016-05-18 15:25:03 +0800295 inode, filp);
296 }
297 }
298}
299
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400300static void ceph_vfs_readpage_complete(struct page *page, void *data, int error)
301{
302 if (!error)
303 SetPageUptodate(page);
304}
305
306static void ceph_vfs_readpage_complete_unlock(struct page *page, void *data, int error)
307{
308 if (!error)
309 SetPageUptodate(page);
310
311 unlock_page(page);
312}
313
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400314static inline bool cache_valid(struct ceph_inode_info *ci)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400315{
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800316 return ci->i_fscache_gen == ci->i_rdcache_gen;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400317}
318
319
320/* Atempt to read from the fscache,
321 *
322 * This function is called from the readpage_nounlock context. DO NOT attempt to
323 * unlock the page here (or in the callback).
324 */
325int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
326{
327 struct ceph_inode_info *ci = ceph_inode(inode);
328 int ret;
329
330 if (!cache_valid(ci))
331 return -ENOBUFS;
332
333 ret = fscache_read_or_alloc_page(ci->fscache, page,
334 ceph_vfs_readpage_complete, NULL,
335 GFP_KERNEL);
336
337 switch (ret) {
338 case 0: /* Page found */
339 dout("page read submitted\n");
340 return 0;
341 case -ENOBUFS: /* Pages were not found, and can't be */
342 case -ENODATA: /* Pages were not found */
343 dout("page/inode not in cache\n");
344 return ret;
345 default:
346 dout("%s: unknown error ret = %i\n", __func__, ret);
347 return ret;
348 }
349}
350
351int ceph_readpages_from_fscache(struct inode *inode,
352 struct address_space *mapping,
353 struct list_head *pages,
354 unsigned *nr_pages)
355{
356 struct ceph_inode_info *ci = ceph_inode(inode);
357 int ret;
358
359 if (!cache_valid(ci))
360 return -ENOBUFS;
361
362 ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
363 ceph_vfs_readpage_complete_unlock,
364 NULL, mapping_gfp_mask(mapping));
365
366 switch (ret) {
367 case 0: /* All pages found */
368 dout("all-page read submitted\n");
369 return 0;
370 case -ENOBUFS: /* Some pages were not found, and can't be */
371 case -ENODATA: /* some pages were not found */
372 dout("page/inode not in cache\n");
373 return ret;
374 default:
375 dout("%s: unknown error ret = %i\n", __func__, ret);
376 return ret;
377 }
378}
379
380void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
381{
382 struct ceph_inode_info *ci = ceph_inode(inode);
383 int ret;
384
Milosz Tanski9b8dd1e2013-09-03 19:11:01 -0400385 if (!PageFsCache(page))
386 return;
387
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400388 if (!cache_valid(ci))
389 return;
390
391 ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
392 if (ret)
393 fscache_uncache_page(ci->fscache, page);
394}
395
396void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
397{
398 struct ceph_inode_info *ci = ceph_inode(inode);
399
Milosz Tanskiffc79662013-09-25 11:18:14 -0400400 if (!PageFsCache(page))
401 return;
402
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400403 fscache_wait_on_page_write(ci->fscache, page);
404 fscache_uncache_page(ci->fscache, page);
405}
406
407void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
408{
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800409 if (fscache_cookie_valid(fsc->fscache)) {
410 struct ceph_fscache_entry *ent;
411 bool found = false;
412
413 mutex_lock(&ceph_fscache_lock);
414 list_for_each_entry(ent, &ceph_fscache_list, list) {
415 if (ent->fscache == fsc->fscache) {
416 list_del(&ent->list);
417 kfree(ent);
418 found = true;
419 break;
420 }
421 }
422 WARN_ON_ONCE(!found);
423 mutex_unlock(&ceph_fscache_lock);
424
425 __fscache_relinquish_cookie(fsc->fscache, 0);
426 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400427 fsc->fscache = NULL;
428}
429
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800430/*
431 * caller should hold CEPH_CAP_FILE_{RD,CACHE}
432 */
433void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400434{
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800435 if (cache_valid(ci))
Milosz Tanskie81568e2013-09-05 18:29:03 +0000436 return;
437
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800438 /* resue i_truncate_mutex. There should be no pending
439 * truncate while the caller holds CEPH_CAP_FILE_RD */
440 mutex_lock(&ci->i_truncate_mutex);
441 if (!cache_valid(ci)) {
442 if (fscache_check_consistency(ci->fscache))
443 fscache_invalidate(ci->fscache);
444 spin_lock(&ci->i_ceph_lock);
445 ci->i_fscache_gen = ci->i_rdcache_gen;
446 spin_unlock(&ci->i_ceph_lock);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400447 }
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +0800448 mutex_unlock(&ci->i_truncate_mutex);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400449}