fscache-index.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <linux/iversion.h>
  19. #include "internal.h"
  20. #include "fscache.h"
  21. #define NFSDBG_FACILITY NFSDBG_FSCACHE
  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. * Register NFS for caching
  33. */
  34. int nfs_fscache_register(void)
  35. {
  36. return fscache_register_netfs(&nfs_fscache_netfs);
  37. }
  38. /*
  39. * Unregister NFS for caching
  40. */
  41. void nfs_fscache_unregister(void)
  42. {
  43. fscache_unregister_netfs(&nfs_fscache_netfs);
  44. }
  45. /*
  46. * Define the server object for FS-Cache. This is used to describe a server
  47. * object to fscache_acquire_cookie(). It is keyed by the NFS protocol and
  48. * server address parameters.
  49. */
  50. const struct fscache_cookie_def nfs_fscache_server_index_def = {
  51. .name = "NFS.server",
  52. .type = FSCACHE_COOKIE_TYPE_INDEX,
  53. };
  54. /*
  55. * Define the superblock object for FS-Cache. This is used to describe a
  56. * superblock object to fscache_acquire_cookie(). It is keyed by all the NFS
  57. * parameters that might cause a separate superblock.
  58. */
  59. const struct fscache_cookie_def nfs_fscache_super_index_def = {
  60. .name = "NFS.super",
  61. .type = FSCACHE_COOKIE_TYPE_INDEX,
  62. };
  63. /*
  64. * Consult the netfs about the state of an object
  65. * - This function can be absent if the index carries no state data
  66. * - The netfs data from the cookie being used as the target is
  67. * presented, as is the auxiliary data
  68. */
  69. static
  70. enum fscache_checkaux nfs_fscache_inode_check_aux(void *cookie_netfs_data,
  71. const void *data,
  72. uint16_t datalen,
  73. loff_t object_size)
  74. {
  75. struct nfs_fscache_inode_auxdata auxdata;
  76. struct nfs_inode *nfsi = cookie_netfs_data;
  77. if (datalen != sizeof(auxdata))
  78. return FSCACHE_CHECKAUX_OBSOLETE;
  79. memset(&auxdata, 0, sizeof(auxdata));
  80. auxdata.mtime = timespec64_to_timespec(nfsi->vfs_inode.i_mtime);
  81. auxdata.ctime = timespec64_to_timespec(nfsi->vfs_inode.i_ctime);
  82. if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
  83. auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
  84. if (memcmp(data, &auxdata, datalen) != 0)
  85. return FSCACHE_CHECKAUX_OBSOLETE;
  86. return FSCACHE_CHECKAUX_OKAY;
  87. }
  88. /*
  89. * Get an extra reference on a read context.
  90. * - This function can be absent if the completion function doesn't require a
  91. * context.
  92. * - The read context is passed back to NFS in the event that a data read on the
  93. * cache fails with EIO - in which case the server must be contacted to
  94. * retrieve the data, which requires the read context for security.
  95. */
  96. static void nfs_fh_get_context(void *cookie_netfs_data, void *context)
  97. {
  98. get_nfs_open_context(context);
  99. }
  100. /*
  101. * Release an extra reference on a read context.
  102. * - This function can be absent if the completion function doesn't require a
  103. * context.
  104. */
  105. static void nfs_fh_put_context(void *cookie_netfs_data, void *context)
  106. {
  107. if (context)
  108. put_nfs_open_context(context);
  109. }
  110. /*
  111. * Define the inode object for FS-Cache. This is used to describe an inode
  112. * object to fscache_acquire_cookie(). It is keyed by the NFS file handle for
  113. * an inode.
  114. *
  115. * Coherency is managed by comparing the copies of i_size, i_mtime and i_ctime
  116. * held in the cache auxiliary data for the data storage object with those in
  117. * the inode struct in memory.
  118. */
  119. const struct fscache_cookie_def nfs_fscache_inode_object_def = {
  120. .name = "NFS.fh",
  121. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  122. .check_aux = nfs_fscache_inode_check_aux,
  123. .get_context = nfs_fh_get_context,
  124. .put_context = nfs_fh_put_context,
  125. };