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