mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-01-09 23:00:21 +00:00
a6b5a28eb5
Change the nfs filesystem to support fscache's indexing rewrite and reenable caching in nfs. The following changes have been made: (1) The fscache_netfs struct is no more, and there's no need to register the filesystem as a whole. (2) The session cookie is now an fscache_volume cookie, allocated with fscache_acquire_volume(). That takes three parameters: a string representing the "volume" in the index, a string naming the cache to use (or NULL) and a u64 that conveys coherency metadata for the volume. For nfs, I've made it render the volume name string as: "nfs,<ver>,<family>,<address>,<port>,<fsidH>,<fsidL>*<,param>[,<uniq>]" (3) The fscache_cookie_def is no more and needed information is passed directly to fscache_acquire_cookie(). The cache no longer calls back into the filesystem, but rather metadata changes are indicated at other times. fscache_acquire_cookie() is passed the same keying and coherency information as before. (4) fscache_enable/disable_cookie() have been removed. Call fscache_use_cookie() and fscache_unuse_cookie() when a file is opened or closed to prevent a cache file from being culled and to keep resources to hand that are needed to do I/O. If a file is opened for writing, we invalidate it with FSCACHE_INVAL_DIO_WRITE in lieu of doing writeback to the cache, thereby making it cease caching until all currently open files are closed. This should give the same behaviour as the uptream code. Making the cache store local modifications isn't straightforward for NFS, so that's left for future patches. (5) fscache_invalidate() now needs to be given uptodate auxiliary data and a file size. It also takes a flag to indicate if this was due to a DIO write. (6) Call nfs_fscache_invalidate() with FSCACHE_INVAL_DIO_WRITE on a file to which a DIO write is made. (7) Call fscache_note_page_release() from nfs_release_page(). (8) Use a killable wait in nfs_vm_page_mkwrite() when waiting for PG_fscache to be cleared. (9) The functions to read and write data to/from the cache are stubbed out pending a conversion to use netfslib. Changes ======= ver #3: - Added missing =n fallback for nfs_fscache_release_file()[1][2]. ver #2: - Use gfpflags_allow_blocking() rather than using flag directly. - fscache_acquire_volume() now returns errors. - Remove NFS_INO_FSCACHE as it's no longer used. - Need to unuse a cookie on file-release, not inode-clear. Signed-off-by: Dave Wysochanski <dwysocha@redhat.com> Co-developed-by: David Howells <dhowells@redhat.com> Signed-off-by: David Howells <dhowells@redhat.com> Tested-by: Dave Wysochanski <dwysocha@redhat.com> Acked-by: Jeff Layton <jlayton@kernel.org> cc: Trond Myklebust <trond.myklebust@hammerspace.com> cc: Anna Schumaker <anna.schumaker@netapp.com> cc: linux-nfs@vger.kernel.org cc: linux-cachefs@redhat.com Link: https://lore.kernel.org/r/202112100804.nksO8K4u-lkp@intel.com/ [1] Link: https://lore.kernel.org/r/202112100957.2oEDT20W-lkp@intel.com/ [2] Link: https://lore.kernel.org/r/163819668938.215744.14448852181937731615.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/163906979003.143852.2601189243864854724.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/163967182112.1823006.7791504655391213379.stgit@warthog.procyon.org.uk/ # v3 Link: https://lore.kernel.org/r/164021575950.640689.12069642327533368467.stgit@warthog.procyon.org.uk/ # v4
297 lines
8.2 KiB
C
297 lines
8.2 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* NFS filesystem cache interface
|
|
*
|
|
* Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/nfs_fs.h>
|
|
#include <linux/nfs_fs_sb.h>
|
|
#include <linux/in6.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/iversion.h>
|
|
|
|
#include "internal.h"
|
|
#include "iostat.h"
|
|
#include "fscache.h"
|
|
|
|
#define NFSDBG_FACILITY NFSDBG_FSCACHE
|
|
|
|
#define NFS_MAX_KEY_LEN 1000
|
|
|
|
static bool nfs_append_int(char *key, int *_len, unsigned long long x)
|
|
{
|
|
if (*_len > NFS_MAX_KEY_LEN)
|
|
return false;
|
|
if (x == 0)
|
|
key[(*_len)++] = ',';
|
|
else
|
|
*_len += sprintf(key + *_len, ",%llx", x);
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Get the per-client index cookie for an NFS client if the appropriate mount
|
|
* flag was set
|
|
* - We always try and get an index cookie for the client, but get filehandle
|
|
* cookies on a per-superblock basis, depending on the mount flags
|
|
*/
|
|
static bool nfs_fscache_get_client_key(struct nfs_client *clp,
|
|
char *key, int *_len)
|
|
{
|
|
const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
|
|
const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
|
|
|
|
*_len += snprintf(key + *_len, NFS_MAX_KEY_LEN - *_len,
|
|
",%u.%u,%x",
|
|
clp->rpc_ops->version,
|
|
clp->cl_minorversion,
|
|
clp->cl_addr.ss_family);
|
|
|
|
switch (clp->cl_addr.ss_family) {
|
|
case AF_INET:
|
|
if (!nfs_append_int(key, _len, sin->sin_port) ||
|
|
!nfs_append_int(key, _len, sin->sin_addr.s_addr))
|
|
return false;
|
|
return true;
|
|
|
|
case AF_INET6:
|
|
if (!nfs_append_int(key, _len, sin6->sin6_port) ||
|
|
!nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[0]) ||
|
|
!nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[1]) ||
|
|
!nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[2]) ||
|
|
!nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[3]))
|
|
return false;
|
|
return true;
|
|
|
|
default:
|
|
printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
|
|
clp->cl_addr.ss_family);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Get the cache cookie for an NFS superblock.
|
|
*
|
|
* The default uniquifier is just an empty string, but it may be overridden
|
|
* either by the 'fsc=xxx' option to mount, or by inheriting it from the parent
|
|
* superblock across an automount point of some nature.
|
|
*/
|
|
int nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
|
|
{
|
|
struct fscache_volume *vcookie;
|
|
struct nfs_server *nfss = NFS_SB(sb);
|
|
unsigned int len = 3;
|
|
char *key;
|
|
|
|
if (uniq) {
|
|
nfss->fscache_uniq = kmemdup_nul(uniq, ulen, GFP_KERNEL);
|
|
if (!nfss->fscache_uniq)
|
|
return -ENOMEM;
|
|
}
|
|
|
|
key = kmalloc(NFS_MAX_KEY_LEN + 24, GFP_KERNEL);
|
|
if (!key)
|
|
return -ENOMEM;
|
|
|
|
memcpy(key, "nfs", 3);
|
|
if (!nfs_fscache_get_client_key(nfss->nfs_client, key, &len) ||
|
|
!nfs_append_int(key, &len, nfss->fsid.major) ||
|
|
!nfs_append_int(key, &len, nfss->fsid.minor) ||
|
|
!nfs_append_int(key, &len, sb->s_flags & NFS_SB_MASK) ||
|
|
!nfs_append_int(key, &len, nfss->flags) ||
|
|
!nfs_append_int(key, &len, nfss->rsize) ||
|
|
!nfs_append_int(key, &len, nfss->wsize) ||
|
|
!nfs_append_int(key, &len, nfss->acregmin) ||
|
|
!nfs_append_int(key, &len, nfss->acregmax) ||
|
|
!nfs_append_int(key, &len, nfss->acdirmin) ||
|
|
!nfs_append_int(key, &len, nfss->acdirmax) ||
|
|
!nfs_append_int(key, &len, nfss->client->cl_auth->au_flavor))
|
|
goto out;
|
|
|
|
if (ulen > 0) {
|
|
if (ulen > NFS_MAX_KEY_LEN - len)
|
|
goto out;
|
|
key[len++] = ',';
|
|
memcpy(key + len, uniq, ulen);
|
|
len += ulen;
|
|
}
|
|
key[len] = 0;
|
|
|
|
/* create a cache index for looking up filehandles */
|
|
vcookie = fscache_acquire_volume(key,
|
|
NULL, /* preferred_cache */
|
|
NULL, 0 /* coherency_data */);
|
|
dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
|
|
nfss, vcookie);
|
|
if (IS_ERR(vcookie)) {
|
|
if (vcookie != ERR_PTR(-EBUSY)) {
|
|
kfree(key);
|
|
return PTR_ERR(vcookie);
|
|
}
|
|
pr_err("NFS: Cache volume key already in use (%s)\n", key);
|
|
vcookie = NULL;
|
|
}
|
|
nfss->fscache = vcookie;
|
|
|
|
out:
|
|
kfree(key);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* release a per-superblock cookie
|
|
*/
|
|
void nfs_fscache_release_super_cookie(struct super_block *sb)
|
|
{
|
|
struct nfs_server *nfss = NFS_SB(sb);
|
|
|
|
dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
|
|
nfss, nfss->fscache);
|
|
|
|
fscache_relinquish_volume(nfss->fscache, NULL, false);
|
|
nfss->fscache = NULL;
|
|
kfree(nfss->fscache_uniq);
|
|
}
|
|
|
|
/*
|
|
* Initialise the per-inode cache cookie pointer for an NFS inode.
|
|
*/
|
|
void nfs_fscache_init_inode(struct inode *inode)
|
|
{
|
|
struct nfs_fscache_inode_auxdata auxdata;
|
|
struct nfs_server *nfss = NFS_SERVER(inode);
|
|
struct nfs_inode *nfsi = NFS_I(inode);
|
|
|
|
nfsi->fscache = NULL;
|
|
if (!(nfss->fscache && S_ISREG(inode->i_mode)))
|
|
return;
|
|
|
|
nfs_fscache_update_auxdata(&auxdata, nfsi);
|
|
|
|
nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
|
|
0,
|
|
nfsi->fh.data, /* index_key */
|
|
nfsi->fh.size,
|
|
&auxdata, /* aux_data */
|
|
sizeof(auxdata),
|
|
i_size_read(&nfsi->vfs_inode));
|
|
}
|
|
|
|
/*
|
|
* Release a per-inode cookie.
|
|
*/
|
|
void nfs_fscache_clear_inode(struct inode *inode)
|
|
{
|
|
struct nfs_inode *nfsi = NFS_I(inode);
|
|
struct fscache_cookie *cookie = nfs_i_fscache(inode);
|
|
|
|
dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
|
|
|
|
fscache_relinquish_cookie(cookie, false);
|
|
nfsi->fscache = NULL;
|
|
}
|
|
|
|
/*
|
|
* Enable or disable caching for a file that is being opened as appropriate.
|
|
* The cookie is allocated when the inode is initialised, but is not enabled at
|
|
* that time. Enablement is deferred to file-open time to avoid stat() and
|
|
* access() thrashing the cache.
|
|
*
|
|
* For now, with NFS, only regular files that are open read-only will be able
|
|
* to use the cache.
|
|
*
|
|
* We enable the cache for an inode if we open it read-only and it isn't
|
|
* currently open for writing. We disable the cache if the inode is open
|
|
* write-only.
|
|
*
|
|
* The caller uses the file struct to pin i_writecount on the inode before
|
|
* calling us when a file is opened for writing, so we can make use of that.
|
|
*
|
|
* Note that this may be invoked multiple times in parallel by parallel
|
|
* nfs_open() functions.
|
|
*/
|
|
void nfs_fscache_open_file(struct inode *inode, struct file *filp)
|
|
{
|
|
struct nfs_fscache_inode_auxdata auxdata;
|
|
struct nfs_inode *nfsi = NFS_I(inode);
|
|
struct fscache_cookie *cookie = nfs_i_fscache(inode);
|
|
bool open_for_write = inode_is_open_for_write(inode);
|
|
|
|
if (!fscache_cookie_valid(cookie))
|
|
return;
|
|
|
|
fscache_use_cookie(cookie, open_for_write);
|
|
if (open_for_write) {
|
|
dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
|
|
nfs_fscache_update_auxdata(&auxdata, nfsi);
|
|
fscache_invalidate(cookie, &auxdata, i_size_read(inode),
|
|
FSCACHE_INVAL_DIO_WRITE);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
|
|
|
|
void nfs_fscache_release_file(struct inode *inode, struct file *filp)
|
|
{
|
|
struct nfs_fscache_inode_auxdata auxdata;
|
|
struct nfs_inode *nfsi = NFS_I(inode);
|
|
struct fscache_cookie *cookie = nfs_i_fscache(inode);
|
|
|
|
if (fscache_cookie_valid(cookie)) {
|
|
nfs_fscache_update_auxdata(&auxdata, nfsi);
|
|
fscache_unuse_cookie(cookie, &auxdata, NULL);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Retrieve a page from fscache
|
|
*/
|
|
int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
|
|
struct inode *inode, struct page *page)
|
|
{
|
|
dfprintk(FSCACHE,
|
|
"NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
|
|
nfs_i_fscache(inode), page, page->index, page->flags, inode);
|
|
|
|
if (PageChecked(page)) {
|
|
ClearPageChecked(page);
|
|
return 1;
|
|
}
|
|
|
|
return -ENOBUFS; // TODO: Use netfslib
|
|
}
|
|
|
|
/*
|
|
* Retrieve a set of pages from fscache
|
|
*/
|
|
int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
|
|
struct inode *inode,
|
|
struct address_space *mapping,
|
|
struct list_head *pages,
|
|
unsigned *nr_pages)
|
|
{
|
|
dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
|
|
nfs_i_fscache(inode), *nr_pages, inode);
|
|
|
|
return -ENOBUFS; // TODO: Use netfslib
|
|
}
|
|
|
|
/*
|
|
* Store a newly fetched page in fscache
|
|
* - PG_fscache must be set on the page
|
|
*/
|
|
void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
|
|
{
|
|
dfprintk(FSCACHE,
|
|
"NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
|
|
nfs_i_fscache(inode), page, page->index, page->flags, sync);
|
|
|
|
return; // TODO: Use netfslib
|
|
}
|