fscache-index.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_sec = nfsi->vfs_inode.i_mtime.tv_sec;
  81. auxdata.mtime_nsec = nfsi->vfs_inode.i_mtime.tv_nsec;
  82. auxdata.ctime_sec = nfsi->vfs_inode.i_ctime.tv_sec;
  83. auxdata.ctime_nsec = nfsi->vfs_inode.i_ctime.tv_nsec;
  84. if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
  85. auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
  86. if (memcmp(data, &auxdata, datalen) != 0)
  87. return FSCACHE_CHECKAUX_OBSOLETE;
  88. return FSCACHE_CHECKAUX_OKAY;
  89. }
  90. /*
  91. * Get an extra reference on a read context.
  92. * - This function can be absent if the completion function doesn't require a
  93. * context.
  94. * - The read context is passed back to NFS in the event that a data read on the
  95. * cache fails with EIO - in which case the server must be contacted to
  96. * retrieve the data, which requires the read context for security.
  97. */
  98. static void nfs_fh_get_context(void *cookie_netfs_data, void *context)
  99. {
  100. get_nfs_open_context(context);
  101. }
  102. /*
  103. * Release an extra reference on a read context.
  104. * - This function can be absent if the completion function doesn't require a
  105. * context.
  106. */
  107. static void nfs_fh_put_context(void *cookie_netfs_data, void *context)
  108. {
  109. if (context)
  110. put_nfs_open_context(context);
  111. }
  112. /*
  113. * Define the inode object for FS-Cache. This is used to describe an inode
  114. * object to fscache_acquire_cookie(). It is keyed by the NFS file handle for
  115. * an inode.
  116. *
  117. * Coherency is managed by comparing the copies of i_size, i_mtime and i_ctime
  118. * held in the cache auxiliary data for the data storage object with those in
  119. * the inode struct in memory.
  120. */
  121. const struct fscache_cookie_def nfs_fscache_inode_object_def = {
  122. .name = "NFS.fh",
  123. .type = FSCACHE_COOKIE_TYPE_DATAFILE,
  124. .check_aux = nfs_fscache_inode_check_aux,
  125. .get_context = nfs_fh_get_context,
  126. .put_context = nfs_fh_put_context,
  127. };