David Howells | 8ec442a | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 1 | /* NFS FS-Cache index structure definition |
| 2 | * |
| 3 | * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/nfs_fs.h> |
| 17 | #include <linux/nfs_fs_sb.h> |
| 18 | #include <linux/in6.h> |
Jeff Layton | 1eb5d98 | 2018-01-09 08:21:17 -0500 | [diff] [blame^] | 19 | #include <linux/iversion.h> |
David Howells | 8ec442a | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 20 | |
| 21 | #include "internal.h" |
| 22 | #include "fscache.h" |
| 23 | |
| 24 | #define NFSDBG_FACILITY NFSDBG_FSCACHE |
| 25 | |
| 26 | /* |
| 27 | * Define the NFS filesystem for FS-Cache. Upon registration FS-Cache sticks |
| 28 | * the cookie for the top-level index object for NFS into here. The top-level |
| 29 | * index can than have other cache objects inserted into it. |
| 30 | */ |
| 31 | struct fscache_netfs nfs_fscache_netfs = { |
| 32 | .name = "nfs", |
| 33 | .version = 0, |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * Register NFS for caching |
| 38 | */ |
| 39 | int nfs_fscache_register(void) |
| 40 | { |
| 41 | return fscache_register_netfs(&nfs_fscache_netfs); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Unregister NFS for caching |
| 46 | */ |
| 47 | void nfs_fscache_unregister(void) |
| 48 | { |
| 49 | fscache_unregister_netfs(&nfs_fscache_netfs); |
| 50 | } |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * Layout of the key for an NFS server cache object. |
| 54 | */ |
| 55 | struct nfs_server_key { |
| 56 | uint16_t nfsversion; /* NFS protocol version */ |
| 57 | uint16_t family; /* address family */ |
| 58 | uint16_t port; /* IP port */ |
| 59 | union { |
| 60 | struct in_addr ipv4_addr; /* IPv4 address */ |
| 61 | struct in6_addr ipv6_addr; /* IPv6 address */ |
| 62 | } addr[0]; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * Generate a key to describe a server in the main NFS index |
| 67 | * - We return the length of the key, or 0 if we can't generate one |
| 68 | */ |
| 69 | static uint16_t nfs_server_get_key(const void *cookie_netfs_data, |
| 70 | void *buffer, uint16_t bufmax) |
| 71 | { |
| 72 | const struct nfs_client *clp = cookie_netfs_data; |
| 73 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr; |
| 74 | const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr; |
| 75 | struct nfs_server_key *key = buffer; |
| 76 | uint16_t len = sizeof(struct nfs_server_key); |
| 77 | |
David Howells | f3f7603 | 2014-09-25 14:34:41 +0100 | [diff] [blame] | 78 | memset(key, 0, len); |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 79 | key->nfsversion = clp->rpc_ops->version; |
| 80 | key->family = clp->cl_addr.ss_family; |
| 81 | |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 82 | switch (clp->cl_addr.ss_family) { |
| 83 | case AF_INET: |
| 84 | key->port = sin->sin_port; |
| 85 | key->addr[0].ipv4_addr = sin->sin_addr; |
| 86 | len += sizeof(key->addr[0].ipv4_addr); |
| 87 | break; |
| 88 | |
| 89 | case AF_INET6: |
| 90 | key->port = sin6->sin6_port; |
| 91 | key->addr[0].ipv6_addr = sin6->sin6_addr; |
| 92 | len += sizeof(key->addr[0].ipv6_addr); |
| 93 | break; |
| 94 | |
| 95 | default: |
| 96 | printk(KERN_WARNING "NFS: Unknown network family '%d'\n", |
| 97 | clp->cl_addr.ss_family); |
| 98 | len = 0; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return len; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Define the server object for FS-Cache. This is used to describe a server |
| 107 | * object to fscache_acquire_cookie(). It is keyed by the NFS protocol and |
| 108 | * server address parameters. |
| 109 | */ |
| 110 | const struct fscache_cookie_def nfs_fscache_server_index_def = { |
| 111 | .name = "NFS.server", |
| 112 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
| 113 | .get_key = nfs_server_get_key, |
| 114 | }; |
David Howells | 0873404 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Generate a key to describe a superblock key in the main NFS index |
| 118 | */ |
| 119 | static uint16_t nfs_super_get_key(const void *cookie_netfs_data, |
| 120 | void *buffer, uint16_t bufmax) |
| 121 | { |
| 122 | const struct nfs_fscache_key *key; |
| 123 | const struct nfs_server *nfss = cookie_netfs_data; |
| 124 | uint16_t len; |
| 125 | |
| 126 | key = nfss->fscache_key; |
| 127 | len = sizeof(key->key) + key->key.uniq_len; |
| 128 | if (len > bufmax) { |
| 129 | len = 0; |
| 130 | } else { |
| 131 | memcpy(buffer, &key->key, sizeof(key->key)); |
| 132 | memcpy(buffer + sizeof(key->key), |
| 133 | key->key.uniquifier, key->key.uniq_len); |
| 134 | } |
| 135 | |
| 136 | return len; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Define the superblock object for FS-Cache. This is used to describe a |
| 141 | * superblock object to fscache_acquire_cookie(). It is keyed by all the NFS |
| 142 | * parameters that might cause a separate superblock. |
| 143 | */ |
| 144 | const struct fscache_cookie_def nfs_fscache_super_index_def = { |
| 145 | .name = "NFS.super", |
| 146 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
| 147 | .get_key = nfs_super_get_key, |
| 148 | }; |
David Howells | 10329a5 | 2009-04-03 16:42:43 +0100 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Definition of the auxiliary data attached to NFS inode storage objects |
| 152 | * within the cache. |
| 153 | * |
| 154 | * The contents of this struct are recorded in the on-disk local cache in the |
| 155 | * auxiliary data attached to the data storage object backing an inode. This |
| 156 | * permits coherency to be managed when a new inode binds to an already extant |
| 157 | * cache object. |
| 158 | */ |
| 159 | struct nfs_fscache_inode_auxdata { |
| 160 | struct timespec mtime; |
| 161 | struct timespec ctime; |
| 162 | loff_t size; |
| 163 | u64 change_attr; |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * Generate a key to describe an NFS inode in an NFS server's index |
| 168 | */ |
| 169 | static uint16_t nfs_fscache_inode_get_key(const void *cookie_netfs_data, |
| 170 | void *buffer, uint16_t bufmax) |
| 171 | { |
| 172 | const struct nfs_inode *nfsi = cookie_netfs_data; |
| 173 | uint16_t nsize; |
| 174 | |
| 175 | /* use the inode's NFS filehandle as the key */ |
| 176 | nsize = nfsi->fh.size; |
| 177 | memcpy(buffer, nfsi->fh.data, nsize); |
| 178 | return nsize; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Get certain file attributes from the netfs data |
| 183 | * - This function can be absent for an index |
| 184 | * - Not permitted to return an error |
| 185 | * - The netfs data from the cookie being used as the source is presented |
| 186 | */ |
| 187 | static void nfs_fscache_inode_get_attr(const void *cookie_netfs_data, |
| 188 | uint64_t *size) |
| 189 | { |
| 190 | const struct nfs_inode *nfsi = cookie_netfs_data; |
| 191 | |
| 192 | *size = nfsi->vfs_inode.i_size; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Get the auxiliary data from netfs data |
| 197 | * - This function can be absent if the index carries no state data |
| 198 | * - Should store the auxiliary data in the buffer |
| 199 | * - Should return the amount of amount stored |
| 200 | * - Not permitted to return an error |
| 201 | * - The netfs data from the cookie being used as the source is presented |
| 202 | */ |
| 203 | static uint16_t nfs_fscache_inode_get_aux(const void *cookie_netfs_data, |
| 204 | void *buffer, uint16_t bufmax) |
| 205 | { |
| 206 | struct nfs_fscache_inode_auxdata auxdata; |
| 207 | const struct nfs_inode *nfsi = cookie_netfs_data; |
| 208 | |
| 209 | memset(&auxdata, 0, sizeof(auxdata)); |
| 210 | auxdata.size = nfsi->vfs_inode.i_size; |
| 211 | auxdata.mtime = nfsi->vfs_inode.i_mtime; |
| 212 | auxdata.ctime = nfsi->vfs_inode.i_ctime; |
| 213 | |
| 214 | if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4) |
Jeff Layton | 1eb5d98 | 2018-01-09 08:21:17 -0500 | [diff] [blame^] | 215 | auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode); |
David Howells | 10329a5 | 2009-04-03 16:42:43 +0100 | [diff] [blame] | 216 | |
| 217 | if (bufmax > sizeof(auxdata)) |
| 218 | bufmax = sizeof(auxdata); |
| 219 | |
| 220 | memcpy(buffer, &auxdata, bufmax); |
| 221 | return bufmax; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Consult the netfs about the state of an object |
| 226 | * - This function can be absent if the index carries no state data |
| 227 | * - The netfs data from the cookie being used as the target is |
| 228 | * presented, as is the auxiliary data |
| 229 | */ |
| 230 | static |
| 231 | enum fscache_checkaux nfs_fscache_inode_check_aux(void *cookie_netfs_data, |
| 232 | const void *data, |
| 233 | uint16_t datalen) |
| 234 | { |
| 235 | struct nfs_fscache_inode_auxdata auxdata; |
| 236 | struct nfs_inode *nfsi = cookie_netfs_data; |
| 237 | |
| 238 | if (datalen != sizeof(auxdata)) |
| 239 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 240 | |
| 241 | memset(&auxdata, 0, sizeof(auxdata)); |
| 242 | auxdata.size = nfsi->vfs_inode.i_size; |
| 243 | auxdata.mtime = nfsi->vfs_inode.i_mtime; |
| 244 | auxdata.ctime = nfsi->vfs_inode.i_ctime; |
| 245 | |
| 246 | if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4) |
Jeff Layton | 1eb5d98 | 2018-01-09 08:21:17 -0500 | [diff] [blame^] | 247 | auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode); |
David Howells | 10329a5 | 2009-04-03 16:42:43 +0100 | [diff] [blame] | 248 | |
| 249 | if (memcmp(data, &auxdata, datalen) != 0) |
| 250 | return FSCACHE_CHECKAUX_OBSOLETE; |
| 251 | |
| 252 | return FSCACHE_CHECKAUX_OKAY; |
| 253 | } |
| 254 | |
| 255 | /* |
David Howells | 1fcdf53 | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 256 | * Get an extra reference on a read context. |
| 257 | * - This function can be absent if the completion function doesn't require a |
| 258 | * context. |
| 259 | * - The read context is passed back to NFS in the event that a data read on the |
| 260 | * cache fails with EIO - in which case the server must be contacted to |
| 261 | * retrieve the data, which requires the read context for security. |
| 262 | */ |
| 263 | static void nfs_fh_get_context(void *cookie_netfs_data, void *context) |
| 264 | { |
| 265 | get_nfs_open_context(context); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Release an extra reference on a read context. |
| 270 | * - This function can be absent if the completion function doesn't require a |
| 271 | * context. |
| 272 | */ |
| 273 | static void nfs_fh_put_context(void *cookie_netfs_data, void *context) |
| 274 | { |
| 275 | if (context) |
| 276 | put_nfs_open_context(context); |
| 277 | } |
| 278 | |
| 279 | /* |
David Howells | 10329a5 | 2009-04-03 16:42:43 +0100 | [diff] [blame] | 280 | * Define the inode object for FS-Cache. This is used to describe an inode |
| 281 | * object to fscache_acquire_cookie(). It is keyed by the NFS file handle for |
| 282 | * an inode. |
| 283 | * |
| 284 | * Coherency is managed by comparing the copies of i_size, i_mtime and i_ctime |
| 285 | * held in the cache auxiliary data for the data storage object with those in |
| 286 | * the inode struct in memory. |
| 287 | */ |
| 288 | const struct fscache_cookie_def nfs_fscache_inode_object_def = { |
| 289 | .name = "NFS.fh", |
| 290 | .type = FSCACHE_COOKIE_TYPE_DATAFILE, |
| 291 | .get_key = nfs_fscache_inode_get_key, |
| 292 | .get_attr = nfs_fscache_inode_get_attr, |
| 293 | .get_aux = nfs_fscache_inode_get_aux, |
| 294 | .check_aux = nfs_fscache_inode_check_aux, |
David Howells | 1fcdf53 | 2009-04-03 16:42:44 +0100 | [diff] [blame] | 295 | .get_context = nfs_fh_get_context, |
| 296 | .put_context = nfs_fh_put_context, |
David Howells | 10329a5 | 2009-04-03 16:42:43 +0100 | [diff] [blame] | 297 | }; |