fscache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* NFS filesystem cache interface definitions
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #ifndef _NFS_FSCACHE_H
  8. #define _NFS_FSCACHE_H
  9. #include <linux/swap.h>
  10. #include <linux/nfs_fs.h>
  11. #include <linux/nfs_mount.h>
  12. #include <linux/nfs4_mount.h>
  13. #include <linux/fscache.h>
  14. #include <linux/iversion.h>
  15. #ifdef CONFIG_NFS_FSCACHE
  16. /*
  17. * Definition of the auxiliary data attached to NFS inode storage objects
  18. * within the cache.
  19. *
  20. * The contents of this struct are recorded in the on-disk local cache in the
  21. * auxiliary data attached to the data storage object backing an inode. This
  22. * permits coherency to be managed when a new inode binds to an already extant
  23. * cache object.
  24. */
  25. struct nfs_fscache_inode_auxdata {
  26. s64 mtime_sec;
  27. s64 mtime_nsec;
  28. s64 ctime_sec;
  29. s64 ctime_nsec;
  30. u64 change_attr;
  31. };
  32. /*
  33. * fscache.c
  34. */
  35. extern int nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
  36. extern void nfs_fscache_release_super_cookie(struct super_block *);
  37. extern void nfs_fscache_init_inode(struct inode *);
  38. extern void nfs_fscache_clear_inode(struct inode *);
  39. extern void nfs_fscache_open_file(struct inode *, struct file *);
  40. extern void nfs_fscache_release_file(struct inode *, struct file *);
  41. extern int __nfs_fscache_read_page(struct inode *, struct page *);
  42. extern void __nfs_fscache_write_page(struct inode *, struct page *);
  43. static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
  44. {
  45. if (folio_test_fscache(folio)) {
  46. if (current_is_kswapd() || !(gfp & __GFP_FS))
  47. return false;
  48. folio_wait_fscache(folio);
  49. fscache_note_page_release(nfs_i_fscache(folio->mapping->host));
  50. nfs_inc_fscache_stats(folio->mapping->host,
  51. NFSIOS_FSCACHE_PAGES_UNCACHED);
  52. }
  53. return true;
  54. }
  55. /*
  56. * Retrieve a page from an inode data storage object.
  57. */
  58. static inline int nfs_fscache_read_page(struct inode *inode, struct page *page)
  59. {
  60. if (nfs_i_fscache(inode))
  61. return __nfs_fscache_read_page(inode, page);
  62. return -ENOBUFS;
  63. }
  64. /*
  65. * Store a page newly fetched from the server in an inode data storage object
  66. * in the cache.
  67. */
  68. static inline void nfs_fscache_write_page(struct inode *inode,
  69. struct page *page)
  70. {
  71. if (nfs_i_fscache(inode))
  72. __nfs_fscache_write_page(inode, page);
  73. }
  74. static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
  75. struct inode *inode)
  76. {
  77. memset(auxdata, 0, sizeof(*auxdata));
  78. auxdata->mtime_sec = inode->i_mtime.tv_sec;
  79. auxdata->mtime_nsec = inode->i_mtime.tv_nsec;
  80. auxdata->ctime_sec = inode->i_ctime.tv_sec;
  81. auxdata->ctime_nsec = inode->i_ctime.tv_nsec;
  82. if (NFS_SERVER(inode)->nfs_client->rpc_ops->version == 4)
  83. auxdata->change_attr = inode_peek_iversion_raw(inode);
  84. }
  85. /*
  86. * Invalidate the contents of fscache for this inode. This will not sleep.
  87. */
  88. static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
  89. {
  90. struct nfs_fscache_inode_auxdata auxdata;
  91. struct nfs_inode *nfsi = NFS_I(inode);
  92. if (nfsi->fscache) {
  93. nfs_fscache_update_auxdata(&auxdata, inode);
  94. fscache_invalidate(nfsi->fscache, &auxdata,
  95. i_size_read(inode), flags);
  96. }
  97. }
  98. /*
  99. * indicate the client caching state as readable text
  100. */
  101. static inline const char *nfs_server_fscache_state(struct nfs_server *server)
  102. {
  103. if (server->fscache)
  104. return "yes";
  105. return "no ";
  106. }
  107. #else /* CONFIG_NFS_FSCACHE */
  108. static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
  109. static inline void nfs_fscache_init_inode(struct inode *inode) {}
  110. static inline void nfs_fscache_clear_inode(struct inode *inode) {}
  111. static inline void nfs_fscache_open_file(struct inode *inode,
  112. struct file *filp) {}
  113. static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
  114. static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
  115. {
  116. return true; /* may release folio */
  117. }
  118. static inline int nfs_fscache_read_page(struct inode *inode, struct page *page)
  119. {
  120. return -ENOBUFS;
  121. }
  122. static inline void nfs_fscache_write_page(struct inode *inode, struct page *page) {}
  123. static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
  124. static inline const char *nfs_server_fscache_state(struct nfs_server *server)
  125. {
  126. return "no ";
  127. }
  128. #endif /* CONFIG_NFS_FSCACHE */
  129. #endif /* _NFS_FSCACHE_H */