2019-05-28 16:57:16 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-08-21 21:29:54 +00:00
|
|
|
/*
|
|
|
|
* Ceph cache definitions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
|
|
|
|
* Written by Milosz Tanski (milosz@adfin.com)
|
|
|
|
*/
|
|
|
|
|
2019-09-05 15:29:29 +00:00
|
|
|
#include <linux/ceph/ceph_debug.h>
|
|
|
|
|
2019-03-25 16:38:32 +00:00
|
|
|
#include <linux/fs_context.h>
|
2013-08-21 21:29:54 +00:00
|
|
|
#include "super.h"
|
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
struct ceph_aux_inode {
|
2018-07-13 20:18:36 +00:00
|
|
|
u64 version;
|
|
|
|
u64 mtime_sec;
|
|
|
|
u64 mtime_nsec;
|
2013-08-21 21:29:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct fscache_netfs ceph_cache_netfs = {
|
|
|
|
.name = "ceph",
|
|
|
|
.version = 0,
|
|
|
|
};
|
|
|
|
|
2017-06-27 03:57:56 +00:00
|
|
|
static DEFINE_MUTEX(ceph_fscache_lock);
|
|
|
|
static LIST_HEAD(ceph_fscache_list);
|
|
|
|
|
|
|
|
struct ceph_fscache_entry {
|
|
|
|
struct list_head list;
|
|
|
|
struct fscache_cookie *fscache;
|
|
|
|
size_t uniq_len;
|
2018-04-04 12:41:28 +00:00
|
|
|
/* The following members must be last */
|
|
|
|
struct ceph_fsid fsid;
|
2020-02-13 16:00:04 +00:00
|
|
|
char uniquifier[];
|
2017-06-27 03:57:56 +00:00
|
|
|
};
|
|
|
|
|
2013-08-21 21:29:54 +00:00
|
|
|
static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
|
|
|
|
.name = "CEPH.fsid",
|
|
|
|
.type = FSCACHE_COOKIE_TYPE_INDEX,
|
|
|
|
};
|
|
|
|
|
2018-03-10 12:32:05 +00:00
|
|
|
int __init ceph_fscache_register(void)
|
2013-08-21 21:29:54 +00:00
|
|
|
{
|
|
|
|
return fscache_register_netfs(&ceph_cache_netfs);
|
|
|
|
}
|
|
|
|
|
2013-09-06 15:13:18 +00:00
|
|
|
void ceph_fscache_unregister(void)
|
2013-08-21 21:29:54 +00:00
|
|
|
{
|
|
|
|
fscache_unregister_netfs(&ceph_cache_netfs);
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:38:32 +00:00
|
|
|
int ceph_fscache_register_fs(struct ceph_fs_client* fsc, struct fs_context *fc)
|
2013-08-21 21:29:54 +00:00
|
|
|
{
|
2017-06-27 03:57:56 +00:00
|
|
|
const struct ceph_fsid *fsid = &fsc->client->fsid;
|
|
|
|
const char *fscache_uniq = fsc->mount_options->fscache_uniq;
|
|
|
|
size_t uniq_len = fscache_uniq ? strlen(fscache_uniq) : 0;
|
|
|
|
struct ceph_fscache_entry *ent;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
mutex_lock(&ceph_fscache_lock);
|
|
|
|
list_for_each_entry(ent, &ceph_fscache_list, list) {
|
|
|
|
if (memcmp(&ent->fsid, fsid, sizeof(*fsid)))
|
|
|
|
continue;
|
|
|
|
if (ent->uniq_len != uniq_len)
|
|
|
|
continue;
|
|
|
|
if (uniq_len && memcmp(ent->uniquifier, fscache_uniq, uniq_len))
|
|
|
|
continue;
|
|
|
|
|
2019-12-22 02:31:52 +00:00
|
|
|
errorfc(fc, "fscache cookie already registered for fsid %pU, use fsc=<uniquifier> option",
|
2019-03-25 16:38:32 +00:00
|
|
|
fsid);
|
2017-06-27 03:57:56 +00:00
|
|
|
err = -EBUSY;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent = kzalloc(sizeof(*ent) + uniq_len, GFP_KERNEL);
|
|
|
|
if (!ent) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2018-04-04 12:41:28 +00:00
|
|
|
memcpy(&ent->fsid, fsid, sizeof(*fsid));
|
|
|
|
if (uniq_len > 0) {
|
|
|
|
memcpy(&ent->uniquifier, fscache_uniq, uniq_len);
|
|
|
|
ent->uniq_len = uniq_len;
|
|
|
|
}
|
|
|
|
|
2013-08-21 21:29:54 +00:00
|
|
|
fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
|
|
|
|
&ceph_fscache_fsid_object_def,
|
2018-04-04 12:41:28 +00:00
|
|
|
&ent->fsid, sizeof(ent->fsid) + uniq_len,
|
|
|
|
NULL, 0,
|
2018-04-04 12:41:28 +00:00
|
|
|
fsc, 0, true);
|
2013-08-21 21:29:54 +00:00
|
|
|
|
2017-06-27 03:57:56 +00:00
|
|
|
if (fsc->fscache) {
|
|
|
|
ent->fscache = fsc->fscache;
|
|
|
|
list_add_tail(&ent->list, &ceph_fscache_list);
|
|
|
|
} else {
|
|
|
|
kfree(ent);
|
2019-12-22 02:31:52 +00:00
|
|
|
errorfc(fc, "unable to register fscache cookie for fsid %pU",
|
2017-06-27 03:57:56 +00:00
|
|
|
fsid);
|
|
|
|
/* all other fs ignore this error */
|
|
|
|
}
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&ceph_fscache_lock);
|
|
|
|
return err;
|
2013-08-21 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum fscache_checkaux ceph_fscache_inode_check_aux(
|
2018-04-04 12:41:28 +00:00
|
|
|
void *cookie_netfs_data, const void *data, uint16_t dlen,
|
|
|
|
loff_t object_size)
|
2013-08-21 21:29:54 +00:00
|
|
|
{
|
|
|
|
struct ceph_aux_inode aux;
|
|
|
|
struct ceph_inode_info* ci = cookie_netfs_data;
|
|
|
|
struct inode* inode = &ci->vfs_inode;
|
|
|
|
|
2018-04-04 12:41:28 +00:00
|
|
|
if (dlen != sizeof(aux) ||
|
|
|
|
i_size_read(inode) != object_size)
|
2013-08-21 21:29:54 +00:00
|
|
|
return FSCACHE_CHECKAUX_OBSOLETE;
|
|
|
|
|
|
|
|
memset(&aux, 0, sizeof(aux));
|
2016-05-20 08:57:29 +00:00
|
|
|
aux.version = ci->i_version;
|
2018-07-13 20:18:36 +00:00
|
|
|
aux.mtime_sec = inode->i_mtime.tv_sec;
|
|
|
|
aux.mtime_nsec = inode->i_mtime.tv_nsec;
|
2013-08-21 21:29:54 +00:00
|
|
|
|
|
|
|
if (memcmp(data, &aux, sizeof(aux)) != 0)
|
|
|
|
return FSCACHE_CHECKAUX_OBSOLETE;
|
|
|
|
|
2018-01-30 08:29:17 +00:00
|
|
|
dout("ceph inode 0x%p cached okay\n", ci);
|
2013-08-21 21:29:54 +00:00
|
|
|
return FSCACHE_CHECKAUX_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
|
|
|
|
.name = "CEPH.inode",
|
|
|
|
.type = FSCACHE_COOKIE_TYPE_DATAFILE,
|
|
|
|
.check_aux = ceph_fscache_inode_check_aux,
|
|
|
|
};
|
|
|
|
|
2016-05-18 07:25:03 +00:00
|
|
|
void ceph_fscache_register_inode_cookie(struct inode *inode)
|
2013-08-21 21:29:54 +00:00
|
|
|
{
|
2016-05-18 07:25:03 +00:00
|
|
|
struct ceph_inode_info *ci = ceph_inode(inode);
|
|
|
|
struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
|
2018-04-04 12:41:28 +00:00
|
|
|
struct ceph_aux_inode aux;
|
2013-08-21 21:29:54 +00:00
|
|
|
|
|
|
|
/* No caching for filesystem */
|
2017-08-20 18:22:02 +00:00
|
|
|
if (!fsc->fscache)
|
2013-08-21 21:29:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Only cache for regular files that are read only */
|
2016-05-18 07:25:03 +00:00
|
|
|
if (!S_ISREG(inode->i_mode))
|
2013-08-21 21:29:54 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-18 07:25:03 +00:00
|
|
|
inode_lock_nested(inode, I_MUTEX_CHILD);
|
|
|
|
if (!ci->fscache) {
|
2018-04-04 12:41:28 +00:00
|
|
|
memset(&aux, 0, sizeof(aux));
|
|
|
|
aux.version = ci->i_version;
|
2018-07-13 20:18:36 +00:00
|
|
|
aux.mtime_sec = inode->i_mtime.tv_sec;
|
|
|
|
aux.mtime_nsec = inode->i_mtime.tv_nsec;
|
2016-05-18 07:25:03 +00:00
|
|
|
ci->fscache = fscache_acquire_cookie(fsc->fscache,
|
2018-04-04 12:41:28 +00:00
|
|
|
&ceph_fscache_inode_object_def,
|
|
|
|
&ci->i_vino, sizeof(ci->i_vino),
|
|
|
|
&aux, sizeof(aux),
|
2018-04-04 12:41:28 +00:00
|
|
|
ci, i_size_read(inode), false);
|
2016-05-18 07:25:03 +00:00
|
|
|
}
|
2016-01-22 20:40:57 +00:00
|
|
|
inode_unlock(inode);
|
2013-08-21 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
|
|
|
|
{
|
|
|
|
struct fscache_cookie* cookie;
|
|
|
|
|
|
|
|
if ((cookie = ci->fscache) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ci->fscache = NULL;
|
|
|
|
|
2018-04-04 12:41:28 +00:00
|
|
|
fscache_relinquish_cookie(cookie, &ci->i_vino, false);
|
2013-08-21 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:03 +00:00
|
|
|
static bool ceph_fscache_can_enable(void *data)
|
|
|
|
{
|
|
|
|
struct inode *inode = data;
|
|
|
|
return !inode_is_open_for_write(inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ceph_fscache_file_set_cookie(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct ceph_inode_info *ci = ceph_inode(inode);
|
|
|
|
|
|
|
|
if (!fscache_cookie_valid(ci->fscache))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (inode_is_open_for_write(inode)) {
|
|
|
|
dout("fscache_file_set_cookie %p %p disabling cache\n",
|
|
|
|
inode, filp);
|
2018-04-04 12:41:28 +00:00
|
|
|
fscache_disable_cookie(ci->fscache, &ci->i_vino, false);
|
2016-05-18 07:25:03 +00:00
|
|
|
} else {
|
2018-04-04 12:41:28 +00:00
|
|
|
fscache_enable_cookie(ci->fscache, &ci->i_vino, i_size_read(inode),
|
2018-04-04 12:41:28 +00:00
|
|
|
ceph_fscache_can_enable, inode);
|
2016-05-18 07:25:03 +00:00
|
|
|
if (fscache_cookie_enabled(ci->fscache)) {
|
2016-12-29 20:19:32 +00:00
|
|
|
dout("fscache_file_set_cookie %p %p enabling cache\n",
|
2016-05-18 07:25:03 +00:00
|
|
|
inode, filp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 21:29:54 +00:00
|
|
|
void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
|
|
|
|
{
|
2017-06-27 03:57:56 +00:00
|
|
|
if (fscache_cookie_valid(fsc->fscache)) {
|
|
|
|
struct ceph_fscache_entry *ent;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
mutex_lock(&ceph_fscache_lock);
|
|
|
|
list_for_each_entry(ent, &ceph_fscache_list, list) {
|
|
|
|
if (ent->fscache == fsc->fscache) {
|
|
|
|
list_del(&ent->list);
|
|
|
|
kfree(ent);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WARN_ON_ONCE(!found);
|
|
|
|
mutex_unlock(&ceph_fscache_lock);
|
|
|
|
|
2018-04-04 12:41:28 +00:00
|
|
|
__fscache_relinquish_cookie(fsc->fscache, NULL, false);
|
2017-06-27 03:57:56 +00:00
|
|
|
}
|
2013-08-21 21:29:54 +00:00
|
|
|
fsc->fscache = NULL;
|
|
|
|
}
|