18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* NFS FS-Cache index structure definition
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/mm.h>
128c2ecf20Sopenharmony_ci#include <linux/nfs_fs.h>
138c2ecf20Sopenharmony_ci#include <linux/nfs_fs_sb.h>
148c2ecf20Sopenharmony_ci#include <linux/in6.h>
158c2ecf20Sopenharmony_ci#include <linux/iversion.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "internal.h"
188c2ecf20Sopenharmony_ci#include "fscache.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define NFSDBG_FACILITY		NFSDBG_FSCACHE
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * Define the NFS filesystem for FS-Cache.  Upon registration FS-Cache sticks
248c2ecf20Sopenharmony_ci * the cookie for the top-level index object for NFS into here.  The top-level
258c2ecf20Sopenharmony_ci * index can than have other cache objects inserted into it.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistruct fscache_netfs nfs_fscache_netfs = {
288c2ecf20Sopenharmony_ci	.name		= "nfs",
298c2ecf20Sopenharmony_ci	.version	= 0,
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * Register NFS for caching
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ciint nfs_fscache_register(void)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	return fscache_register_netfs(&nfs_fscache_netfs);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Unregister NFS for caching
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_civoid nfs_fscache_unregister(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	fscache_unregister_netfs(&nfs_fscache_netfs);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Define the server object for FS-Cache.  This is used to describe a server
508c2ecf20Sopenharmony_ci * object to fscache_acquire_cookie().  It is keyed by the NFS protocol and
518c2ecf20Sopenharmony_ci * server address parameters.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ciconst struct fscache_cookie_def nfs_fscache_server_index_def = {
548c2ecf20Sopenharmony_ci	.name		= "NFS.server",
558c2ecf20Sopenharmony_ci	.type 		= FSCACHE_COOKIE_TYPE_INDEX,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Define the superblock object for FS-Cache.  This is used to describe a
608c2ecf20Sopenharmony_ci * superblock object to fscache_acquire_cookie().  It is keyed by all the NFS
618c2ecf20Sopenharmony_ci * parameters that might cause a separate superblock.
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ciconst struct fscache_cookie_def nfs_fscache_super_index_def = {
648c2ecf20Sopenharmony_ci	.name		= "NFS.super",
658c2ecf20Sopenharmony_ci	.type 		= FSCACHE_COOKIE_TYPE_INDEX,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * Consult the netfs about the state of an object
708c2ecf20Sopenharmony_ci * - This function can be absent if the index carries no state data
718c2ecf20Sopenharmony_ci * - The netfs data from the cookie being used as the target is
728c2ecf20Sopenharmony_ci *   presented, as is the auxiliary data
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_cistatic
758c2ecf20Sopenharmony_cienum fscache_checkaux nfs_fscache_inode_check_aux(void *cookie_netfs_data,
768c2ecf20Sopenharmony_ci						  const void *data,
778c2ecf20Sopenharmony_ci						  uint16_t datalen,
788c2ecf20Sopenharmony_ci						  loff_t object_size)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct nfs_fscache_inode_auxdata auxdata;
818c2ecf20Sopenharmony_ci	struct nfs_inode *nfsi = cookie_netfs_data;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (datalen != sizeof(auxdata))
848c2ecf20Sopenharmony_ci		return FSCACHE_CHECKAUX_OBSOLETE;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	memset(&auxdata, 0, sizeof(auxdata));
878c2ecf20Sopenharmony_ci	auxdata.mtime_sec  = nfsi->vfs_inode.i_mtime.tv_sec;
888c2ecf20Sopenharmony_ci	auxdata.mtime_nsec = nfsi->vfs_inode.i_mtime.tv_nsec;
898c2ecf20Sopenharmony_ci	auxdata.ctime_sec  = nfsi->vfs_inode.i_ctime.tv_sec;
908c2ecf20Sopenharmony_ci	auxdata.ctime_nsec = nfsi->vfs_inode.i_ctime.tv_nsec;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
938c2ecf20Sopenharmony_ci		auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (memcmp(data, &auxdata, datalen) != 0)
968c2ecf20Sopenharmony_ci		return FSCACHE_CHECKAUX_OBSOLETE;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return FSCACHE_CHECKAUX_OKAY;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * Get an extra reference on a read context.
1038c2ecf20Sopenharmony_ci * - This function can be absent if the completion function doesn't require a
1048c2ecf20Sopenharmony_ci *   context.
1058c2ecf20Sopenharmony_ci * - The read context is passed back to NFS in the event that a data read on the
1068c2ecf20Sopenharmony_ci *   cache fails with EIO - in which case the server must be contacted to
1078c2ecf20Sopenharmony_ci *   retrieve the data, which requires the read context for security.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistatic void nfs_fh_get_context(void *cookie_netfs_data, void *context)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	get_nfs_open_context(context);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/*
1158c2ecf20Sopenharmony_ci * Release an extra reference on a read context.
1168c2ecf20Sopenharmony_ci * - This function can be absent if the completion function doesn't require a
1178c2ecf20Sopenharmony_ci *   context.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistatic void nfs_fh_put_context(void *cookie_netfs_data, void *context)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	if (context)
1228c2ecf20Sopenharmony_ci		put_nfs_open_context(context);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/*
1268c2ecf20Sopenharmony_ci * Define the inode object for FS-Cache.  This is used to describe an inode
1278c2ecf20Sopenharmony_ci * object to fscache_acquire_cookie().  It is keyed by the NFS file handle for
1288c2ecf20Sopenharmony_ci * an inode.
1298c2ecf20Sopenharmony_ci *
1308c2ecf20Sopenharmony_ci * Coherency is managed by comparing the copies of i_size, i_mtime and i_ctime
1318c2ecf20Sopenharmony_ci * held in the cache auxiliary data for the data storage object with those in
1328c2ecf20Sopenharmony_ci * the inode struct in memory.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ciconst struct fscache_cookie_def nfs_fscache_inode_object_def = {
1358c2ecf20Sopenharmony_ci	.name		= "NFS.fh",
1368c2ecf20Sopenharmony_ci	.type		= FSCACHE_COOKIE_TYPE_DATAFILE,
1378c2ecf20Sopenharmony_ci	.check_aux	= nfs_fscache_inode_check_aux,
1388c2ecf20Sopenharmony_ci	.get_context	= nfs_fh_get_context,
1398c2ecf20Sopenharmony_ci	.put_context	= nfs_fh_put_context,
1408c2ecf20Sopenharmony_ci};
141