^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Ceph cache definitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written by Milosz Tanski (milosz@adfin.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ceph/ceph_debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "super.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct ceph_aux_inode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u64 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u64 mtime_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u64 mtime_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct fscache_netfs ceph_cache_netfs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .name = "ceph",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .version = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static DEFINE_MUTEX(ceph_fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static LIST_HEAD(ceph_fscache_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct ceph_fscache_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct fscache_cookie *fscache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) size_t uniq_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* The following members must be last */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct ceph_fsid fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) char uniquifier[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .name = "CEPH.fsid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .type = FSCACHE_COOKIE_TYPE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int __init ceph_fscache_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return fscache_register_netfs(&ceph_cache_netfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void ceph_fscache_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) fscache_unregister_netfs(&ceph_cache_netfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const struct ceph_fsid *fsid = &fsc->client->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const char *fscache_uniq = fsc->mount_options->fscache_uniq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct ceph_fscache_entry *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) mutex_lock(&ceph_fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) list_for_each_entry(ent, &ceph_fscache_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ent->uniq_len != uniq_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) errorfc(fc, "fscache cookie already registered for fsid %pU, use fsc=<uniquifier> option",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!ent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) memcpy(&ent->fsid, fsid, sizeof(*fsid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (uniq_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ent->uniq_len = uniq_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) &ceph_fscache_fsid_object_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) &ent->fsid, sizeof(ent->fsid) + uniq_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fsc, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (fsc->fscache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ent->fscache = fsc->fscache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) list_add_tail(&ent->list, &ceph_fscache_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kfree(ent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) errorfc(fc, "unable to register fscache cookie for fsid %pU",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* all other fs ignore this error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) mutex_unlock(&ceph_fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static enum fscache_checkaux ceph_fscache_inode_check_aux(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void *cookie_netfs_data, const void *data, uint16_t dlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) loff_t object_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct ceph_aux_inode aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct ceph_inode_info* ci = cookie_netfs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct inode* inode = &ci->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (dlen != sizeof(aux) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) i_size_read(inode) != object_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) memset(&aux, 0, sizeof(aux));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) aux.version = ci->i_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) aux.mtime_sec = inode->i_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) aux.mtime_nsec = inode->i_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (memcmp(data, &aux, sizeof(aux)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dout("ceph inode 0x%p cached okay\n", ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return FSCACHE_CHECKAUX_OKAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .name = "CEPH.inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .type = FSCACHE_COOKIE_TYPE_DATAFILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .check_aux = ceph_fscache_inode_check_aux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void ceph_fscache_register_inode_cookie(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct ceph_aux_inode aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* No caching for filesystem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (!fsc->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Only cache for regular files that are read only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) inode_lock_nested(inode, I_MUTEX_CHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!ci->fscache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) memset(&aux, 0, sizeof(aux));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) aux.version = ci->i_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) aux.mtime_sec = inode->i_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) aux.mtime_nsec = inode->i_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ci->fscache = fscache_acquire_cookie(fsc->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) &ceph_fscache_inode_object_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) &ci->i_vino, sizeof(ci->i_vino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) &aux, sizeof(aux),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ci, i_size_read(inode), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct fscache_cookie* cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if ((cookie = ci->fscache) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ci->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) fscache_relinquish_cookie(cookie, &ci->i_vino, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static bool ceph_fscache_can_enable(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct inode *inode = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return !inode_is_open_for_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!fscache_cookie_valid(ci->fscache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (inode_is_open_for_write(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dout("fscache_file_set_cookie %p %p disabling cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) fscache_uncache_all_inode_pages(ci->fscache, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ceph_fscache_can_enable, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (fscache_cookie_enabled(ci->fscache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dout("fscache_file_set_cookie %p %p enabling cache\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static inline bool cache_valid(struct ceph_inode_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return ci->i_fscache_gen == ci->i_rdcache_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* Atempt to read from the fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * This function is called from the readpage_nounlock context. DO NOT attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * unlock the page here (or in the callback).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (!cache_valid(ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = fscache_read_or_alloc_page(ci->fscache, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ceph_readpage_from_fscache_complete, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) case 0: /* Page found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) dout("page read submitted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) case -ENOBUFS: /* Pages were not found, and can't be */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) case -ENODATA: /* Pages were not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dout("page/inode not in cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) dout("%s: unknown error ret = %i\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int ceph_readpages_from_fscache(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct list_head *pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) unsigned *nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!cache_valid(ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ceph_readpage_from_fscache_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) NULL, mapping_gfp_mask(mapping));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case 0: /* All pages found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dout("all-page read submitted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) case -ENOBUFS: /* Some pages were not found, and can't be */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) case -ENODATA: /* some pages were not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) dout("page/inode not in cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) dout("%s: unknown error ret = %i\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!PageFsCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!cache_valid(ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = fscache_write_page(ci->fscache, page, i_size_read(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) fscache_uncache_page(ci->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct ceph_inode_info *ci = ceph_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!PageFsCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) fscache_wait_on_page_write(ci->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) fscache_uncache_page(ci->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (fscache_cookie_valid(fsc->fscache)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct ceph_fscache_entry *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) mutex_lock(&ceph_fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) list_for_each_entry(ent, &ceph_fscache_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (ent->fscache == fsc->fscache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) list_del(&ent->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) kfree(ent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) WARN_ON_ONCE(!found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) mutex_unlock(&ceph_fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __fscache_relinquish_cookie(fsc->fscache, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) fsc->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * caller should hold CEPH_CAP_FILE_{RD,CACHE}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) void ceph_fscache_revalidate_cookie(struct ceph_inode_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (cache_valid(ci))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* resue i_truncate_mutex. There should be no pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * truncate while the caller holds CEPH_CAP_FILE_RD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) mutex_lock(&ci->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!cache_valid(ci)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (fscache_check_consistency(ci->fscache, &ci->i_vino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) fscache_invalidate(ci->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) spin_lock(&ci->i_ceph_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ci->i_fscache_gen = ci->i_rdcache_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) spin_unlock(&ci->i_ceph_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) mutex_unlock(&ci->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }