Thomas Gleixner | 1f32761 | 2019-05-28 09:57:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 2 | /* |
| 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 Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
Ilya Dryomov | 48f930e | 2019-09-05 17:29:29 +0200 | [diff] [blame] | 9 | #include <linux/ceph/ceph_debug.h> |
| 10 | |
David Howells | 82995cc | 2019-03-25 16:38:32 +0000 | [diff] [blame] | 11 | #include <linux/fs_context.h> |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 12 | #include "super.h" |
| 13 | #include "cache.h" |
| 14 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 15 | struct fscache_netfs ceph_cache_netfs = { |
| 16 | .name = "ceph", |
| 17 | .version = 0, |
| 18 | }; |
| 19 | |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 20 | static DEFINE_MUTEX(ceph_fscache_lock); |
| 21 | static LIST_HEAD(ceph_fscache_list); |
| 22 | |
| 23 | struct ceph_fscache_entry { |
| 24 | struct list_head list; |
| 25 | struct fscache_cookie *fscache; |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 26 | size_t uniq_len; |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 27 | /* The following members must be last */ |
| 28 | struct ceph_fsid fsid; |
Gustavo A. R. Silva | f682dc7 | 2020-02-13 10:00:04 -0600 | [diff] [blame] | 29 | char uniquifier[]; |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 30 | }; |
| 31 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 32 | static const struct fscache_cookie_def ceph_fscache_fsid_object_def = { |
| 33 | .name = "CEPH.fsid", |
| 34 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 35 | }; |
| 36 | |
Chengguang Xu | 57a35df | 2018-03-10 20:32:05 +0800 | [diff] [blame] | 37 | int __init ceph_fscache_register(void) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 38 | { |
| 39 | return fscache_register_netfs(&ceph_cache_netfs); |
| 40 | } |
| 41 | |
Milosz Tanski | 971f0bd | 2013-09-06 15:13:18 +0000 | [diff] [blame] | 42 | void ceph_fscache_unregister(void) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 43 | { |
| 44 | fscache_unregister_netfs(&ceph_cache_netfs); |
| 45 | } |
| 46 | |
David Howells | 82995cc | 2019-03-25 16:38:32 +0000 | [diff] [blame] | 47 | int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 48 | { |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 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; |
| 53 | int err = 0; |
| 54 | |
| 55 | mutex_lock(&ceph_fscache_lock); |
| 56 | list_for_each_entry(ent, &ceph_fscache_list, list) { |
| 57 | if (memcmp(&ent->fsid, fsid, sizeof(*fsid))) |
| 58 | continue; |
| 59 | if (ent->uniq_len != uniq_len) |
| 60 | continue; |
| 61 | if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len)) |
| 62 | continue; |
| 63 | |
Al Viro | d53d0f7 | 2019-12-21 21:31:52 -0500 | [diff] [blame] | 64 | errorfc(fc, "fscache cookie already registered for fsid %pU, use fsc=<uniquifier> option", |
David Howells | 82995cc | 2019-03-25 16:38:32 +0000 | [diff] [blame] | 65 | fsid); |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 66 | err = -EBUSY; |
| 67 | goto out_unlock; |
| 68 | } |
| 69 | |
| 70 | ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL); |
| 71 | if (!ent) { |
| 72 | err = -ENOMEM; |
| 73 | goto out_unlock; |
| 74 | } |
| 75 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 76 | memcpy(&ent->fsid, fsid, sizeof(*fsid)); |
| 77 | if (uniq_len > 0) { |
| 78 | memcpy(&ent->uniquifier, fscache_uniq, uniq_len); |
| 79 | ent->uniq_len = uniq_len; |
| 80 | } |
| 81 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 82 | fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index, |
| 83 | &ceph_fscache_fsid_object_def, |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 84 | &ent->fsid, sizeof(ent->fsid) + uniq_len, |
| 85 | NULL, 0, |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 86 | fsc, 0, true); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 87 | |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 88 | if (fsc->fscache) { |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 89 | ent->fscache = fsc->fscache; |
| 90 | list_add_tail(&ent->list, &ceph_fscache_list); |
| 91 | } else { |
| 92 | kfree(ent); |
Al Viro | d53d0f7 | 2019-12-21 21:31:52 -0500 | [diff] [blame] | 93 | errorfc(fc, "unable to register fscache cookie for fsid %pU", |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 94 | fsid); |
| 95 | /* all other fs ignore this error */ |
| 96 | } |
| 97 | out_unlock: |
| 98 | mutex_unlock(&ceph_fscache_lock); |
| 99 | return err; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 102 | static enum fscache_checkaux ceph_fscache_inode_check_aux( |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 103 | void *cookie_netfs_data, const void *data, uint16_t dlen, |
| 104 | loff_t object_size) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 105 | { |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 106 | struct ceph_inode_info* ci = cookie_netfs_data; |
| 107 | struct inode* inode = &ci->vfs_inode; |
| 108 | |
Jeff Layton | 25b7351 | 2021-05-05 15:21:12 -0400 | [diff] [blame] | 109 | if (dlen != sizeof(ci->i_version) || |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 110 | i_size_read(inode) != object_size) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 111 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 112 | |
Jeff Layton | 25b7351 | 2021-05-05 15:21:12 -0400 | [diff] [blame] | 113 | if (*(u64 *)data != ci->i_version) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 114 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 115 | |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 116 | dout("ceph inode 0x%p cached okay\n", ci); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 117 | return FSCACHE_CHECKAUX_OKAY; |
| 118 | } |
| 119 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 120 | static const struct fscache_cookie_def ceph_fscache_inode_object_def = { |
| 121 | .name = "CEPH.inode", |
| 122 | .type = FSCACHE_COOKIE_TYPE_DATAFILE, |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 123 | .check_aux = ceph_fscache_inode_check_aux, |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 126 | void ceph_fscache_register_inode_cookie(struct inode *inode) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 127 | { |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 128 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 129 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 130 | |
| 131 | /* No caching for filesystem */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 132 | if (!fsc->fscache) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 133 | return; |
| 134 | |
| 135 | /* Only cache for regular files that are read only */ |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 136 | if (!S_ISREG(inode->i_mode)) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 137 | return; |
| 138 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 139 | inode_lock_nested(inode, I_MUTEX_CHILD); |
| 140 | if (!ci->fscache) { |
| 141 | ci->fscache = fscache_acquire_cookie(fsc->fscache, |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 142 | &ceph_fscache_inode_object_def, |
| 143 | &ci->i_vino, sizeof(ci->i_vino), |
Jeff Layton | 25b7351 | 2021-05-05 15:21:12 -0400 | [diff] [blame] | 144 | &ci->i_version, sizeof(ci->i_version), |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 145 | ci, i_size_read(inode), false); |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 146 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 147 | inode_unlock(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci) |
| 151 | { |
| 152 | struct fscache_cookie* cookie; |
| 153 | |
| 154 | if ((cookie = ci->fscache) == NULL) |
| 155 | return; |
| 156 | |
| 157 | ci->fscache = NULL; |
| 158 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 159 | fscache_relinquish_cookie(cookie, &ci->i_vino, false); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 162 | static bool ceph_fscache_can_enable(void *data) |
| 163 | { |
| 164 | struct inode *inode = data; |
| 165 | return !inode_is_open_for_write(inode); |
| 166 | } |
| 167 | |
| 168 | void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp) |
| 169 | { |
| 170 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 171 | |
| 172 | if (!fscache_cookie_valid(ci->fscache)) |
| 173 | return; |
| 174 | |
| 175 | if (inode_is_open_for_write(inode)) { |
| 176 | dout("fscache_file_set_cookie %p %p disabling cache\n", |
| 177 | inode, filp); |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 178 | fscache_disable_cookie(ci->fscache, &ci->i_vino, false); |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 179 | } else { |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 180 | fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode), |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 181 | ceph_fscache_can_enable, inode); |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 182 | if (fscache_cookie_enabled(ci->fscache)) { |
Colin Ian King | 0fbc536 | 2016-12-29 20:19:32 +0000 | [diff] [blame] | 183 | dout("fscache_file_set_cookie %p %p enabling cache\n", |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 184 | inode, filp); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 189 | void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc) |
| 190 | { |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 191 | if (fscache_cookie_valid(fsc->fscache)) { |
| 192 | struct ceph_fscache_entry *ent; |
| 193 | bool found = false; |
| 194 | |
| 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); |
| 199 | kfree(ent); |
| 200 | found = true; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | WARN_ON_ONCE(!found); |
| 205 | mutex_unlock(&ceph_fscache_lock); |
| 206 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 207 | __fscache_relinquish_cookie(fsc->fscache, NULL, false); |
Yan, Zheng | 1d8f836 | 2017-06-27 11:57:56 +0800 | [diff] [blame] | 208 | } |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 209 | fsc->fscache = NULL; |
| 210 | } |