11f327613SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 299ccbd22SMilosz Tanski /* 399ccbd22SMilosz Tanski * Ceph cache definitions. 499ccbd22SMilosz Tanski * 599ccbd22SMilosz Tanski * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved. 699ccbd22SMilosz Tanski * Written by Milosz Tanski (milosz@adfin.com) 799ccbd22SMilosz Tanski */ 899ccbd22SMilosz Tanski 948f930eaSIlya Dryomov #include <linux/ceph/ceph_debug.h> 1048f930eaSIlya Dryomov 1182995cc6SDavid Howells #include <linux/fs_context.h> 1299ccbd22SMilosz Tanski #include "super.h" 1399ccbd22SMilosz Tanski #include "cache.h" 1499ccbd22SMilosz Tanski 15400e1286SJeff Layton void ceph_fscache_register_inode_cookie(struct inode *inode) 1699ccbd22SMilosz Tanski { 17400e1286SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 18400e1286SJeff Layton struct ceph_fs_client *fsc = ceph_inode_to_client(inode); 19400e1286SJeff Layton 20400e1286SJeff Layton /* No caching for filesystem? */ 21400e1286SJeff Layton if (!fsc->fscache) 22400e1286SJeff Layton return; 23400e1286SJeff Layton 24400e1286SJeff Layton /* Regular files only */ 25400e1286SJeff Layton if (!S_ISREG(inode->i_mode)) 26400e1286SJeff Layton return; 27400e1286SJeff Layton 28400e1286SJeff Layton /* Only new inodes! */ 29400e1286SJeff Layton if (!(inode->i_state & I_NEW)) 30400e1286SJeff Layton return; 31400e1286SJeff Layton 32*bc899ee1SDavid Howells WARN_ON_ONCE(ci->netfs_ctx.cache); 33400e1286SJeff Layton 34*bc899ee1SDavid Howells ci->netfs_ctx.cache = 35*bc899ee1SDavid Howells fscache_acquire_cookie(fsc->fscache, 0, 36400e1286SJeff Layton &ci->i_vino, sizeof(ci->i_vino), 37400e1286SJeff Layton &ci->i_version, sizeof(ci->i_version), 38400e1286SJeff Layton i_size_read(inode)); 3999ccbd22SMilosz Tanski } 4099ccbd22SMilosz Tanski 41400e1286SJeff Layton void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info *ci) 4299ccbd22SMilosz Tanski { 43*bc899ee1SDavid Howells fscache_relinquish_cookie(ceph_fscache_cookie(ci), false); 44400e1286SJeff Layton } 45400e1286SJeff Layton 46400e1286SJeff Layton void ceph_fscache_use_cookie(struct inode *inode, bool will_modify) 47400e1286SJeff Layton { 48400e1286SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 49400e1286SJeff Layton 50*bc899ee1SDavid Howells fscache_use_cookie(ceph_fscache_cookie(ci), will_modify); 51400e1286SJeff Layton } 52400e1286SJeff Layton 53400e1286SJeff Layton void ceph_fscache_unuse_cookie(struct inode *inode, bool update) 54400e1286SJeff Layton { 55400e1286SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 56400e1286SJeff Layton 57400e1286SJeff Layton if (update) { 58400e1286SJeff Layton loff_t i_size = i_size_read(inode); 59400e1286SJeff Layton 60*bc899ee1SDavid Howells fscache_unuse_cookie(ceph_fscache_cookie(ci), 61*bc899ee1SDavid Howells &ci->i_version, &i_size); 62400e1286SJeff Layton } else { 63*bc899ee1SDavid Howells fscache_unuse_cookie(ceph_fscache_cookie(ci), NULL, NULL); 64400e1286SJeff Layton } 65400e1286SJeff Layton } 66400e1286SJeff Layton 67400e1286SJeff Layton void ceph_fscache_update(struct inode *inode) 68400e1286SJeff Layton { 69400e1286SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 70400e1286SJeff Layton loff_t i_size = i_size_read(inode); 71400e1286SJeff Layton 72*bc899ee1SDavid Howells fscache_update_cookie(ceph_fscache_cookie(ci), &ci->i_version, &i_size); 73400e1286SJeff Layton } 74400e1286SJeff Layton 75400e1286SJeff Layton void ceph_fscache_invalidate(struct inode *inode, bool dio_write) 76400e1286SJeff Layton { 77400e1286SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode); 78400e1286SJeff Layton 79*bc899ee1SDavid Howells fscache_invalidate(ceph_fscache_cookie(ci), 80400e1286SJeff Layton &ci->i_version, i_size_read(inode), 81400e1286SJeff Layton dio_write ? FSCACHE_INVAL_DIO_WRITE : 0); 8299ccbd22SMilosz Tanski } 8399ccbd22SMilosz Tanski 8482995cc6SDavid Howells int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc) 8599ccbd22SMilosz Tanski { 861d8f8360SYan, Zheng const struct ceph_fsid *fsid = &fsc->client->fsid; 871d8f8360SYan, Zheng const char *fscache_uniq = fsc->mount_options->fscache_uniq; 881d8f8360SYan, Zheng size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0; 89400e1286SJeff Layton char *name; 901d8f8360SYan, Zheng int err = 0; 911d8f8360SYan, Zheng 92400e1286SJeff Layton name = kasprintf(GFP_KERNEL, "ceph,%pU%s%s", fsid, uniq_len ? "," : "", 93400e1286SJeff Layton uniq_len ? fscache_uniq : ""); 94400e1286SJeff Layton if (!name) 95400e1286SJeff Layton return -ENOMEM; 961d8f8360SYan, Zheng 97400e1286SJeff Layton fsc->fscache = fscache_acquire_volume(name, NULL, NULL, 0); 98400e1286SJeff Layton if (IS_ERR_OR_NULL(fsc->fscache)) { 99400e1286SJeff Layton errorfc(fc, "Unable to register fscache cookie for %s", name); 100400e1286SJeff Layton err = fsc->fscache ? PTR_ERR(fsc->fscache) : -EOPNOTSUPP; 101400e1286SJeff Layton fsc->fscache = NULL; 1021d8f8360SYan, Zheng } 103400e1286SJeff Layton kfree(name); 1041d8f8360SYan, Zheng return err; 10599ccbd22SMilosz Tanski } 10699ccbd22SMilosz Tanski 10799ccbd22SMilosz Tanski void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc) 10899ccbd22SMilosz Tanski { 109400e1286SJeff Layton fscache_relinquish_volume(fsc->fscache, NULL, false); 11099ccbd22SMilosz Tanski } 111