Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 1 | /* |
| 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 Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 24 | #include "super.h" |
| 25 | #include "cache.h" |
| 26 | |
| 27 | struct ceph_aux_inode { |
Yan, Zheng | f6973c0 | 2016-05-20 16:57:29 +0800 | [diff] [blame] | 28 | u64 version; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 29 | struct timespec mtime; |
| 30 | loff_t size; |
| 31 | }; |
| 32 | |
| 33 | struct fscache_netfs ceph_cache_netfs = { |
| 34 | .name = "ceph", |
| 35 | .version = 0, |
| 36 | }; |
| 37 | |
| 38 | static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data, |
| 39 | void *buffer, uint16_t maxbuf) |
| 40 | { |
| 41 | const struct ceph_fs_client* fsc = cookie_netfs_data; |
| 42 | uint16_t klen; |
| 43 | |
| 44 | klen = sizeof(fsc->client->fsid); |
| 45 | if (klen > maxbuf) |
| 46 | return 0; |
| 47 | |
| 48 | memcpy(buffer, &fsc->client->fsid, klen); |
| 49 | return klen; |
| 50 | } |
| 51 | |
| 52 | static const struct fscache_cookie_def ceph_fscache_fsid_object_def = { |
| 53 | .name = "CEPH.fsid", |
| 54 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
| 55 | .get_key = ceph_fscache_session_get_key, |
| 56 | }; |
| 57 | |
Milosz Tanski | 971f0bd | 2013-09-06 15:13:18 +0000 | [diff] [blame] | 58 | int ceph_fscache_register(void) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 59 | { |
| 60 | return fscache_register_netfs(&ceph_cache_netfs); |
| 61 | } |
| 62 | |
Milosz Tanski | 971f0bd | 2013-09-06 15:13:18 +0000 | [diff] [blame] | 63 | void ceph_fscache_unregister(void) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 64 | { |
| 65 | fscache_unregister_netfs(&ceph_cache_netfs); |
| 66 | } |
| 67 | |
| 68 | int ceph_fscache_register_fs(struct ceph_fs_client* fsc) |
| 69 | { |
| 70 | fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index, |
| 71 | &ceph_fscache_fsid_object_def, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 72 | fsc, true); |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 73 | if (!fsc->fscache) |
Colin Ian King | 679f0b8 | 2016-06-23 14:45:24 +0100 | [diff] [blame] | 74 | pr_err("Unable to register fsid: %p fscache cookie\n", fsc); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data, |
| 80 | void *buffer, uint16_t maxbuf) |
| 81 | { |
| 82 | const struct ceph_inode_info* ci = cookie_netfs_data; |
| 83 | uint16_t klen; |
| 84 | |
Geliang Tang | 1291fb9 | 2015-09-30 11:41:05 +0800 | [diff] [blame] | 85 | /* use ceph virtual inode (id + snapshot) */ |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 86 | klen = sizeof(ci->i_vino); |
| 87 | if (klen > maxbuf) |
| 88 | return 0; |
| 89 | |
| 90 | memcpy(buffer, &ci->i_vino, klen); |
| 91 | return klen; |
| 92 | } |
| 93 | |
| 94 | static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data, |
| 95 | void *buffer, uint16_t bufmax) |
| 96 | { |
| 97 | struct ceph_aux_inode aux; |
| 98 | const struct ceph_inode_info* ci = cookie_netfs_data; |
| 99 | const struct inode* inode = &ci->vfs_inode; |
| 100 | |
| 101 | memset(&aux, 0, sizeof(aux)); |
Yan, Zheng | f6973c0 | 2016-05-20 16:57:29 +0800 | [diff] [blame] | 102 | aux.version = ci->i_version; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 103 | aux.mtime = inode->i_mtime; |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 104 | aux.size = i_size_read(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 105 | |
| 106 | memcpy(buffer, &aux, sizeof(aux)); |
| 107 | |
| 108 | return sizeof(aux); |
| 109 | } |
| 110 | |
| 111 | static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data, |
| 112 | uint64_t *size) |
| 113 | { |
| 114 | const struct ceph_inode_info* ci = cookie_netfs_data; |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 115 | *size = i_size_read(&ci->vfs_inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static enum fscache_checkaux ceph_fscache_inode_check_aux( |
| 119 | void *cookie_netfs_data, const void *data, uint16_t dlen) |
| 120 | { |
| 121 | struct ceph_aux_inode aux; |
| 122 | struct ceph_inode_info* ci = cookie_netfs_data; |
| 123 | struct inode* inode = &ci->vfs_inode; |
| 124 | |
| 125 | if (dlen != sizeof(aux)) |
| 126 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 127 | |
| 128 | memset(&aux, 0, sizeof(aux)); |
Yan, Zheng | f6973c0 | 2016-05-20 16:57:29 +0800 | [diff] [blame] | 129 | aux.version = ci->i_version; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 130 | aux.mtime = inode->i_mtime; |
Yan, Zheng | 99c88e6 | 2015-12-30 11:32:46 +0800 | [diff] [blame] | 131 | aux.size = i_size_read(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 132 | |
| 133 | if (memcmp(data, &aux, sizeof(aux)) != 0) |
| 134 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 135 | |
| 136 | dout("ceph inode 0x%p cached okay", ci); |
| 137 | return FSCACHE_CHECKAUX_OKAY; |
| 138 | } |
| 139 | |
| 140 | static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data) |
| 141 | { |
| 142 | struct ceph_inode_info* ci = cookie_netfs_data; |
| 143 | struct pagevec pvec; |
| 144 | pgoff_t first; |
| 145 | int loop, nr_pages; |
| 146 | |
| 147 | pagevec_init(&pvec, 0); |
| 148 | first = 0; |
| 149 | |
| 150 | dout("ceph inode 0x%p now uncached", ci); |
| 151 | |
| 152 | while (1) { |
| 153 | nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first, |
| 154 | PAGEVEC_SIZE - pagevec_count(&pvec)); |
| 155 | |
| 156 | if (!nr_pages) |
| 157 | break; |
| 158 | |
| 159 | for (loop = 0; loop < nr_pages; loop++) |
| 160 | ClearPageFsCache(pvec.pages[loop]); |
| 161 | |
| 162 | first = pvec.pages[nr_pages - 1]->index + 1; |
| 163 | |
| 164 | pvec.nr = nr_pages; |
| 165 | pagevec_release(&pvec); |
| 166 | cond_resched(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static const struct fscache_cookie_def ceph_fscache_inode_object_def = { |
| 171 | .name = "CEPH.inode", |
| 172 | .type = FSCACHE_COOKIE_TYPE_DATAFILE, |
| 173 | .get_key = ceph_fscache_inode_get_key, |
| 174 | .get_attr = ceph_fscache_inode_get_attr, |
| 175 | .get_aux = ceph_fscache_inode_get_aux, |
| 176 | .check_aux = ceph_fscache_inode_check_aux, |
| 177 | .now_uncached = ceph_fscache_inode_now_uncached, |
| 178 | }; |
| 179 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 180 | void ceph_fscache_register_inode_cookie(struct inode *inode) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 181 | { |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 182 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 183 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 184 | |
| 185 | /* No caching for filesystem */ |
| 186 | if (fsc->fscache == NULL) |
| 187 | return; |
| 188 | |
| 189 | /* Only cache for regular files that are read only */ |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 190 | if (!S_ISREG(inode->i_mode)) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 191 | return; |
| 192 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 193 | inode_lock_nested(inode, I_MUTEX_CHILD); |
| 194 | if (!ci->fscache) { |
| 195 | ci->fscache = fscache_acquire_cookie(fsc->fscache, |
| 196 | &ceph_fscache_inode_object_def, |
| 197 | ci, false); |
| 198 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 199 | inode_unlock(inode); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci) |
| 203 | { |
| 204 | struct fscache_cookie* cookie; |
| 205 | |
| 206 | if ((cookie = ci->fscache) == NULL) |
| 207 | return; |
| 208 | |
| 209 | ci->fscache = NULL; |
| 210 | |
| 211 | fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode); |
| 212 | fscache_relinquish_cookie(cookie, 0); |
| 213 | } |
| 214 | |
Yan, Zheng | 46b59b2 | 2016-05-18 15:25:03 +0800 | [diff] [blame] | 215 | static bool ceph_fscache_can_enable(void *data) |
| 216 | { |
| 217 | struct inode *inode = data; |
| 218 | return !inode_is_open_for_write(inode); |
| 219 | } |
| 220 | |
| 221 | void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp) |
| 222 | { |
| 223 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 224 | |
| 225 | if (!fscache_cookie_valid(ci->fscache)) |
| 226 | return; |
| 227 | |
| 228 | if (inode_is_open_for_write(inode)) { |
| 229 | dout("fscache_file_set_cookie %p %p disabling cache\n", |
| 230 | inode, filp); |
| 231 | fscache_disable_cookie(ci->fscache, false); |
| 232 | fscache_uncache_all_inode_pages(ci->fscache, inode); |
| 233 | } else { |
| 234 | fscache_enable_cookie(ci->fscache, ceph_fscache_can_enable, |
| 235 | inode); |
| 236 | if (fscache_cookie_enabled(ci->fscache)) { |
| 237 | dout("fscache_file_set_cookie %p %p enabing cache\n", |
| 238 | inode, filp); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Yan, Zheng | 7158492 | 2017-08-04 11:22:31 +0800 | [diff] [blame] | 243 | static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 244 | { |
| 245 | if (!error) |
| 246 | SetPageUptodate(page); |
| 247 | |
| 248 | unlock_page(page); |
| 249 | } |
| 250 | |
Zhang Zhuoyu | 3b33f69 | 2016-03-25 05:18:39 -0400 | [diff] [blame] | 251 | static inline bool cache_valid(struct ceph_inode_info *ci) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 252 | { |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 253 | return ci->i_fscache_gen == ci->i_rdcache_gen; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | /* Atempt to read from the fscache, |
| 258 | * |
| 259 | * This function is called from the readpage_nounlock context. DO NOT attempt to |
| 260 | * unlock the page here (or in the callback). |
| 261 | */ |
| 262 | int ceph_readpage_from_fscache(struct inode *inode, struct page *page) |
| 263 | { |
| 264 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 265 | int ret; |
| 266 | |
| 267 | if (!cache_valid(ci)) |
| 268 | return -ENOBUFS; |
| 269 | |
| 270 | ret = fscache_read_or_alloc_page(ci->fscache, page, |
Yan, Zheng | 7158492 | 2017-08-04 11:22:31 +0800 | [diff] [blame] | 271 | ceph_readpage_from_fscache_complete, NULL, |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 272 | GFP_KERNEL); |
| 273 | |
| 274 | switch (ret) { |
| 275 | case 0: /* Page found */ |
| 276 | dout("page read submitted\n"); |
| 277 | return 0; |
| 278 | case -ENOBUFS: /* Pages were not found, and can't be */ |
| 279 | case -ENODATA: /* Pages were not found */ |
| 280 | dout("page/inode not in cache\n"); |
| 281 | return ret; |
| 282 | default: |
| 283 | dout("%s: unknown error ret = %i\n", __func__, ret); |
| 284 | return ret; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | int ceph_readpages_from_fscache(struct inode *inode, |
| 289 | struct address_space *mapping, |
| 290 | struct list_head *pages, |
| 291 | unsigned *nr_pages) |
| 292 | { |
| 293 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 294 | int ret; |
| 295 | |
| 296 | if (!cache_valid(ci)) |
| 297 | return -ENOBUFS; |
| 298 | |
| 299 | ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages, |
Yan, Zheng | 7158492 | 2017-08-04 11:22:31 +0800 | [diff] [blame] | 300 | ceph_readpage_from_fscache_complete, |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 301 | NULL, mapping_gfp_mask(mapping)); |
| 302 | |
| 303 | switch (ret) { |
| 304 | case 0: /* All pages found */ |
| 305 | dout("all-page read submitted\n"); |
| 306 | return 0; |
| 307 | case -ENOBUFS: /* Some pages were not found, and can't be */ |
| 308 | case -ENODATA: /* some pages were not found */ |
| 309 | dout("page/inode not in cache\n"); |
| 310 | return ret; |
| 311 | default: |
| 312 | dout("%s: unknown error ret = %i\n", __func__, ret); |
| 313 | return ret; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void ceph_readpage_to_fscache(struct inode *inode, struct page *page) |
| 318 | { |
| 319 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 320 | int ret; |
| 321 | |
Milosz Tanski | 9b8dd1e | 2013-09-03 19:11:01 -0400 | [diff] [blame] | 322 | if (!PageFsCache(page)) |
| 323 | return; |
| 324 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 325 | if (!cache_valid(ci)) |
| 326 | return; |
| 327 | |
| 328 | ret = fscache_write_page(ci->fscache, page, GFP_KERNEL); |
| 329 | if (ret) |
| 330 | fscache_uncache_page(ci->fscache, page); |
| 331 | } |
| 332 | |
| 333 | void ceph_invalidate_fscache_page(struct inode* inode, struct page *page) |
| 334 | { |
| 335 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 336 | |
Milosz Tanski | ffc7966 | 2013-09-25 11:18:14 -0400 | [diff] [blame] | 337 | if (!PageFsCache(page)) |
| 338 | return; |
| 339 | |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 340 | fscache_wait_on_page_write(ci->fscache, page); |
| 341 | fscache_uncache_page(ci->fscache, page); |
| 342 | } |
| 343 | |
| 344 | void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc) |
| 345 | { |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 346 | fscache_relinquish_cookie(fsc->fscache, 0); |
| 347 | fsc->fscache = NULL; |
| 348 | } |
| 349 | |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 350 | /* |
| 351 | * caller should hold CEPH_CAP_FILE_{RD,CACHE} |
| 352 | */ |
| 353 | void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci) |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 354 | { |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 355 | if (cache_valid(ci)) |
Milosz Tanski | e81568e | 2013-09-05 18:29:03 +0000 | [diff] [blame] | 356 | return; |
| 357 | |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 358 | /* 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)) { |
| 362 | if (fscache_check_consistency(ci->fscache)) |
| 363 | 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 Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 367 | } |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 368 | mutex_unlock(&ci->i_truncate_mutex); |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 369 | } |