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