fscache-index.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_fs_sb.h>
  17. #include <linux/in6.h>
  18. #include "internal.h"
  19. #include "fscache.h"
  20. #define NFSDBG_FACILITY NFSDBG_FSCACHE
  21. /*
  22. * Define the NFS filesystem for FS-Cache. Upon registration FS-Cache sticks
  23. * the cookie for the top-level index object for NFS into here. The top-level
  24. * index can than have other cache objects inserted into it.
  25. */
  26. struct fscache_netfs nfs_fscache_netfs = {
  27. .name = "nfs",
  28. .version = 0,
  29. };
  30. /*
  31. * Register NFS for caching
  32. */
  33. int nfs_fscache_register(void)
  34. {
  35. return fscache_register_netfs(&nfs_fscache_netfs);
  36. }
  37. /*
  38. * Unregister NFS for caching
  39. */
  40. void nfs_fscache_unregister(void)
  41. {
  42. fscache_unregister_netfs(&nfs_fscache_netfs);
  43. }
  44. /*
  45. * Layout of the key for an NFS server cache object.
  46. */
  47. struct nfs_server_key {
  48. uint16_t nfsversion; /* NFS protocol version */
  49. uint16_t family; /* address family */
  50. uint16_t port; /* IP port */
  51. union {
  52. struct in_addr ipv4_addr; /* IPv4 address */
  53. struct in6_addr ipv6_addr; /* IPv6 address */
  54. } addr[0];
  55. };
  56. /*
  57. * Generate a key to describe a server in the main NFS index
  58. * - We return the length of the key, or 0 if we can't generate one
  59. */
  60. static uint16_t nfs_server_get_key(const void *cookie_netfs_data,
  61. void *buffer, uint16_t bufmax)
  62. {
  63. const struct nfs_client *clp = cookie_netfs_data;
  64. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
  65. const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
  66. struct nfs_server_key *key = buffer;
  67. uint16_t len = sizeof(struct nfs_server_key);
  68. key->nfsversion = clp->rpc_ops->version;
  69. key->family = clp->cl_addr.ss_family;
  70. memset(key, 0, len);
  71. switch (clp->cl_addr.ss_family) {
  72. case AF_INET:
  73. key->port = sin->sin_port;
  74. key->addr[0].ipv4_addr = sin->sin_addr;
  75. len += sizeof(key->addr[0].ipv4_addr);
  76. break;
  77. case AF_INET6:
  78. key->port = sin6->sin6_port;
  79. key->addr[0].ipv6_addr = sin6->sin6_addr;
  80. len += sizeof(key->addr[0].ipv6_addr);
  81. break;
  82. default:
  83. printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
  84. clp->cl_addr.ss_family);
  85. len = 0;
  86. break;
  87. }
  88. return len;
  89. }
  90. /*
  91. * Define the server object for FS-Cache. This is used to describe a server
  92. * object to fscache_acquire_cookie(). It is keyed by the NFS protocol and
  93. * server address parameters.
  94. */
  95. const struct fscache_cookie_def nfs_fscache_server_index_def = {
  96. .name = "NFS.server",
  97. .type = FSCACHE_COOKIE_TYPE_INDEX,
  98. .get_key = nfs_server_get_key,
  99. };