blob: 7d22850623efd0041b9e16e682ba7f479d581c7a [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Milosz Tanski99ccbd22013-08-21 17:29:54 -04002/*
3 * Ceph cache definitions.
4 *
5 * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
6 * Written by Milosz Tanski (milosz@adfin.com)
Milosz Tanski99ccbd22013-08-21 17:29:54 -04007 */
8
Ilya Dryomov48f930e2019-09-05 17:29:29 +02009#include <linux/ceph/ceph_debug.h>
10
David Howells82995cc2019-03-25 16:38:32 +000011#include <linux/fs_context.h>
Milosz Tanski99ccbd22013-08-21 17:29:54 -040012#include "super.h"
13#include "cache.h"
14
Jeff Layton400e1282021-12-07 08:44:50 -050015void ceph_fscache_register_inode_cookie(struct inode *inode)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040016{
Jeff Layton400e1282021-12-07 08:44:50 -050017 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->fscache);
33
34 ci->fscache = fscache_acquire_cookie(fsc->fscache, 0,
35 &ci->i_vino, sizeof(ci->i_vino),
36 &ci->i_version, sizeof(ci->i_version),
37 i_size_read(inode));
Milosz Tanski99ccbd22013-08-21 17:29:54 -040038}
39
Jeff Layton400e1282021-12-07 08:44:50 -050040void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040041{
Jeff Layton400e1282021-12-07 08:44:50 -050042 struct fscache_cookie *cookie = ci->fscache;
43
44 fscache_relinquish_cookie(cookie, false);
45}
46
47void ceph_fscache_use_cookie(struct inode *inode, bool will_modify)
48{
49 struct ceph_inode_info *ci = ceph_inode(inode);
50
51 fscache_use_cookie(ci->fscache, will_modify);
52}
53
54void ceph_fscache_unuse_cookie(struct inode *inode, bool update)
55{
56 struct ceph_inode_info *ci = ceph_inode(inode);
57
58 if (update) {
59 loff_t i_size = i_size_read(inode);
60
61 fscache_unuse_cookie(ci->fscache, &ci->i_version, &i_size);
62 } else {
63 fscache_unuse_cookie(ci->fscache, NULL, NULL);
64 }
65}
66
67void 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(ci->fscache, &ci->i_version, &i_size);
73}
74
75void ceph_fscache_invalidate(struct inode *inode, bool dio_write)
76{
77 struct ceph_inode_info *ci = ceph_inode(inode);
78
79 fscache_invalidate(ceph_inode(inode)->fscache,
80 &ci->i_version, i_size_read(inode),
81 dio_write ? FSCACHE_INVAL_DIO_WRITE : 0);
Milosz Tanski99ccbd22013-08-21 17:29:54 -040082}
83
David Howells82995cc2019-03-25 16:38:32 +000084int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
Milosz Tanski99ccbd22013-08-21 17:29:54 -040085{
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;
Jeff Layton400e1282021-12-07 08:44:50 -050089 char *name;
Yan, Zheng1d8f8362017-06-27 11:57:56 +080090 int err = 0;
91
Jeff Layton400e1282021-12-07 08:44:50 -050092 name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "",
93 uniq_len ? fscache_uniq : "");
94 if (!name)
95 return -ENOMEM;
Yan, Zheng1d8f8362017-06-27 11:57:56 +080096
Jeff Layton400e1282021-12-07 08:44:50 -050097 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;
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800102 }
Jeff Layton400e1282021-12-07 08:44:50 -0500103 kfree(name);
Yan, Zheng1d8f8362017-06-27 11:57:56 +0800104 return err;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400105}
106
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400107void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
108{
Jeff Layton400e1282021-12-07 08:44:50 -0500109 fscache_relinquish_volume(fsc->fscache, NULL, false);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400110}