fscache.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 (dhowells@redhat.com)
  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. struct nfs_netfs_io_data {
  33. /*
  34. * NFS may split a netfs_io_subrequest into multiple RPCs, each
  35. * with their own read completion. In netfs, we can only call
  36. * netfs_subreq_terminated() once for each subrequest. Use the
  37. * refcount here to double as a marker of the last RPC completion,
  38. * and only call netfs via netfs_subreq_terminated() once.
  39. */
  40. refcount_t refcount;
  41. struct netfs_io_subrequest *sreq;
  42. /*
  43. * Final disposition of the netfs_io_subrequest, sent in
  44. * netfs_subreq_terminated()
  45. */
  46. atomic64_t transferred;
  47. int error;
  48. };
  49. static inline void nfs_netfs_get(struct nfs_netfs_io_data *netfs)
  50. {
  51. refcount_inc(&netfs->refcount);
  52. }
  53. static inline void nfs_netfs_put(struct nfs_netfs_io_data *netfs)
  54. {
  55. /* Only the last RPC completion should call netfs_subreq_terminated() */
  56. if (!refcount_dec_and_test(&netfs->refcount))
  57. return;
  58. /*
  59. * The NFS pageio interface may read a complete page, even when netfs
  60. * only asked for a partial page. Specifically, this may be seen when
  61. * one thread is truncating a file while another one is reading the last
  62. * page of the file.
  63. * Correct the final length here to be no larger than the netfs subrequest
  64. * length, and thus avoid netfs's "Subreq overread" warning message.
  65. */
  66. netfs->sreq->transferred = min_t(s64, netfs->sreq->len,
  67. atomic64_read(&netfs->transferred));
  68. netfs_read_subreq_terminated(netfs->sreq, netfs->error, false);
  69. kfree(netfs);
  70. }
  71. static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi)
  72. {
  73. netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops, false);
  74. }
  75. extern void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr);
  76. extern void nfs_netfs_read_completion(struct nfs_pgio_header *hdr);
  77. extern int nfs_netfs_folio_unlock(struct folio *folio);
  78. /*
  79. * fscache.c
  80. */
  81. extern int nfs_fscache_get_super_cookie(struct super_block *, const char *, int);
  82. extern void nfs_fscache_release_super_cookie(struct super_block *);
  83. extern void nfs_fscache_init_inode(struct inode *);
  84. extern void nfs_fscache_clear_inode(struct inode *);
  85. extern void nfs_fscache_open_file(struct inode *, struct file *);
  86. extern void nfs_fscache_release_file(struct inode *, struct file *);
  87. extern int nfs_netfs_readahead(struct readahead_control *ractl);
  88. extern int nfs_netfs_read_folio(struct file *file, struct folio *folio);
  89. static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
  90. {
  91. if (folio_test_private_2(folio)) { /* [DEPRECATED] */
  92. if (current_is_kswapd() || !(gfp & __GFP_FS))
  93. return false;
  94. folio_wait_private_2(folio);
  95. }
  96. fscache_note_page_release(netfs_i_cookie(netfs_inode(folio->mapping->host)));
  97. return true;
  98. }
  99. static inline void nfs_fscache_update_auxdata(struct nfs_fscache_inode_auxdata *auxdata,
  100. struct inode *inode)
  101. {
  102. memset(auxdata, 0, sizeof(*auxdata));
  103. auxdata->mtime_sec = inode_get_mtime(inode).tv_sec;
  104. auxdata->mtime_nsec = inode_get_mtime(inode).tv_nsec;
  105. auxdata->ctime_sec = inode_get_ctime(inode).tv_sec;
  106. auxdata->ctime_nsec = inode_get_ctime(inode).tv_nsec;
  107. if (NFS_SERVER(inode)->nfs_client->rpc_ops->version == 4)
  108. auxdata->change_attr = inode_peek_iversion_raw(inode);
  109. }
  110. /*
  111. * Invalidate the contents of fscache for this inode. This will not sleep.
  112. */
  113. static inline void nfs_fscache_invalidate(struct inode *inode, int flags)
  114. {
  115. struct nfs_fscache_inode_auxdata auxdata;
  116. struct fscache_cookie *cookie = netfs_i_cookie(&NFS_I(inode)->netfs);
  117. nfs_fscache_update_auxdata(&auxdata, inode);
  118. fscache_invalidate(cookie, &auxdata, i_size_read(inode), flags);
  119. }
  120. /*
  121. * indicate the client caching state as readable text
  122. */
  123. static inline const char *nfs_server_fscache_state(struct nfs_server *server)
  124. {
  125. if (server->fscache)
  126. return "yes";
  127. return "no ";
  128. }
  129. static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
  130. struct nfs_pageio_descriptor *desc)
  131. {
  132. hdr->netfs = desc->pg_netfs;
  133. }
  134. static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
  135. struct nfs_pgio_header *hdr)
  136. {
  137. desc->pg_netfs = hdr->netfs;
  138. }
  139. static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc)
  140. {
  141. desc->pg_netfs = NULL;
  142. }
  143. #else /* CONFIG_NFS_FSCACHE */
  144. static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi) {}
  145. static inline void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr) {}
  146. static inline void nfs_netfs_read_completion(struct nfs_pgio_header *hdr) {}
  147. static inline int nfs_netfs_folio_unlock(struct folio *folio)
  148. {
  149. return 1;
  150. }
  151. static inline void nfs_fscache_release_super_cookie(struct super_block *sb) {}
  152. static inline void nfs_fscache_init_inode(struct inode *inode) {}
  153. static inline void nfs_fscache_clear_inode(struct inode *inode) {}
  154. static inline void nfs_fscache_open_file(struct inode *inode,
  155. struct file *filp) {}
  156. static inline void nfs_fscache_release_file(struct inode *inode, struct file *file) {}
  157. static inline int nfs_netfs_readahead(struct readahead_control *ractl)
  158. {
  159. return -ENOBUFS;
  160. }
  161. static inline int nfs_netfs_read_folio(struct file *file, struct folio *folio)
  162. {
  163. return -ENOBUFS;
  164. }
  165. static inline bool nfs_fscache_release_folio(struct folio *folio, gfp_t gfp)
  166. {
  167. return true; /* may release folio */
  168. }
  169. static inline void nfs_fscache_invalidate(struct inode *inode, int flags) {}
  170. static inline const char *nfs_server_fscache_state(struct nfs_server *server)
  171. {
  172. return "no ";
  173. }
  174. static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
  175. struct nfs_pageio_descriptor *desc) {}
  176. static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
  177. struct nfs_pgio_header *hdr) {}
  178. static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc) {}
  179. #endif /* CONFIG_NFS_FSCACHE */
  180. #endif /* _NFS_FSCACHE_H */