^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* NFS FS-Cache index structure definition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/nfs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/nfs_fs_sb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "fscache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define NFSDBG_FACILITY NFSDBG_FSCACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Define the NFS filesystem for FS-Cache. Upon registration FS-Cache sticks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * the cookie for the top-level index object for NFS into here. The top-level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * index can than have other cache objects inserted into it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct fscache_netfs nfs_fscache_netfs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .name = "nfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .version = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Register NFS for caching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int nfs_fscache_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return fscache_register_netfs(&nfs_fscache_netfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Unregister NFS for caching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void nfs_fscache_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fscache_unregister_netfs(&nfs_fscache_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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Define the server object for FS-Cache. This is used to describe a server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * object to fscache_acquire_cookie(). It is keyed by the NFS protocol and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * server address parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const struct fscache_cookie_def nfs_fscache_server_index_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .name = "NFS.server",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .type = FSCACHE_COOKIE_TYPE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Define the superblock object for FS-Cache. This is used to describe a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * superblock object to fscache_acquire_cookie(). It is keyed by all the NFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * parameters that might cause a separate superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) const struct fscache_cookie_def nfs_fscache_super_index_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .name = "NFS.super",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .type = FSCACHE_COOKIE_TYPE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Consult the netfs about the state of an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * - This function can be absent if the index carries no state data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * - The netfs data from the cookie being used as the target is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * presented, as is the auxiliary data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) enum fscache_checkaux nfs_fscache_inode_check_aux(void *cookie_netfs_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) uint16_t datalen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) loff_t object_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct nfs_fscache_inode_auxdata auxdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct nfs_inode *nfsi = cookie_netfs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (datalen != sizeof(auxdata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) memset(&auxdata, 0, sizeof(auxdata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) auxdata.mtime_sec = nfsi->vfs_inode.i_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) auxdata.mtime_nsec = nfsi->vfs_inode.i_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) auxdata.ctime_sec = nfsi->vfs_inode.i_ctime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) auxdata.ctime_nsec = nfsi->vfs_inode.i_ctime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (memcmp(data, &auxdata, datalen) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return FSCACHE_CHECKAUX_OKAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Get an extra reference on a read context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * - This function can be absent if the completion function doesn't require a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * - The read context is passed back to NFS in the event that a data read on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * cache fails with EIO - in which case the server must be contacted to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * retrieve the data, which requires the read context for security.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void nfs_fh_get_context(void *cookie_netfs_data, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) get_nfs_open_context(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * Release an extra reference on a read context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * - This function can be absent if the completion function doesn't require a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void nfs_fh_put_context(void *cookie_netfs_data, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) put_nfs_open_context(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Define the inode object for FS-Cache. This is used to describe an inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * object to fscache_acquire_cookie(). It is keyed by the NFS file handle for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Coherency is managed by comparing the copies of i_size, i_mtime and i_ctime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * held in the cache auxiliary data for the data storage object with those in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * the inode struct in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) const struct fscache_cookie_def nfs_fscache_inode_object_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .name = "NFS.fh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .type = FSCACHE_COOKIE_TYPE_DATAFILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .check_aux = nfs_fscache_inode_check_aux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .get_context = nfs_fh_get_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .put_context = nfs_fh_put_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) };