fscache.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * CIFS filesystem cache interface
  4. *
  5. * Copyright (c) 2010 Novell, Inc.
  6. * Author(s): Suresh Jayaraman <[email protected]>
  7. *
  8. */
  9. #include "fscache.h"
  10. #include "cifsglob.h"
  11. #include "cifs_debug.h"
  12. #include "cifs_fs_sb.h"
  13. #include "cifsproto.h"
  14. static void cifs_fscache_fill_volume_coherency(
  15. struct cifs_tcon *tcon,
  16. struct cifs_fscache_volume_coherency_data *cd)
  17. {
  18. memset(cd, 0, sizeof(*cd));
  19. cd->resource_id = cpu_to_le64(tcon->resource_id);
  20. cd->vol_create_time = tcon->vol_create_time;
  21. cd->vol_serial_number = cpu_to_le32(tcon->vol_serial_number);
  22. }
  23. int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
  24. {
  25. struct cifs_fscache_volume_coherency_data cd;
  26. struct TCP_Server_Info *server = tcon->ses->server;
  27. struct fscache_volume *vcookie;
  28. const struct sockaddr *sa = (struct sockaddr *)&server->dstaddr;
  29. size_t slen, i;
  30. char *sharename;
  31. char *key;
  32. int ret = -ENOMEM;
  33. tcon->fscache = NULL;
  34. switch (sa->sa_family) {
  35. case AF_INET:
  36. case AF_INET6:
  37. break;
  38. default:
  39. cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
  40. return -EINVAL;
  41. }
  42. memset(&key, 0, sizeof(key));
  43. sharename = extract_sharename(tcon->tree_name);
  44. if (IS_ERR(sharename)) {
  45. cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
  46. return PTR_ERR(sharename);
  47. }
  48. slen = strlen(sharename);
  49. for (i = 0; i < slen; i++)
  50. if (sharename[i] == '/')
  51. sharename[i] = ';';
  52. key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
  53. if (!key)
  54. goto out;
  55. cifs_fscache_fill_volume_coherency(tcon, &cd);
  56. vcookie = fscache_acquire_volume(key,
  57. NULL, /* preferred_cache */
  58. &cd, sizeof(cd));
  59. cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
  60. if (IS_ERR(vcookie)) {
  61. if (vcookie != ERR_PTR(-EBUSY)) {
  62. ret = PTR_ERR(vcookie);
  63. goto out_2;
  64. }
  65. pr_err("Cache volume key already in use (%s)\n", key);
  66. vcookie = NULL;
  67. }
  68. tcon->fscache = vcookie;
  69. ret = 0;
  70. out_2:
  71. kfree(key);
  72. out:
  73. kfree(sharename);
  74. return ret;
  75. }
  76. void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
  77. {
  78. struct cifs_fscache_volume_coherency_data cd;
  79. cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
  80. cifs_fscache_fill_volume_coherency(tcon, &cd);
  81. fscache_relinquish_volume(tcon->fscache, &cd, false);
  82. tcon->fscache = NULL;
  83. }
  84. void cifs_fscache_get_inode_cookie(struct inode *inode)
  85. {
  86. struct cifs_fscache_inode_coherency_data cd;
  87. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  88. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  89. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  90. cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
  91. cifsi->netfs.cache =
  92. fscache_acquire_cookie(tcon->fscache, 0,
  93. &cifsi->uniqueid, sizeof(cifsi->uniqueid),
  94. &cd, sizeof(cd),
  95. i_size_read(&cifsi->netfs.inode));
  96. }
  97. void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
  98. {
  99. if (update) {
  100. struct cifs_fscache_inode_coherency_data cd;
  101. loff_t i_size = i_size_read(inode);
  102. cifs_fscache_fill_coherency(inode, &cd);
  103. fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
  104. } else {
  105. fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
  106. }
  107. }
  108. void cifs_fscache_release_inode_cookie(struct inode *inode)
  109. {
  110. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  111. struct fscache_cookie *cookie = cifs_inode_cookie(inode);
  112. if (cookie) {
  113. cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
  114. fscache_relinquish_cookie(cookie, false);
  115. cifsi->netfs.cache = NULL;
  116. }
  117. }
  118. /*
  119. * Fallback page reading interface.
  120. */
  121. static int fscache_fallback_read_page(struct inode *inode, struct page *page)
  122. {
  123. struct netfs_cache_resources cres;
  124. struct fscache_cookie *cookie = cifs_inode_cookie(inode);
  125. struct iov_iter iter;
  126. struct bio_vec bvec[1];
  127. int ret;
  128. memset(&cres, 0, sizeof(cres));
  129. bvec[0].bv_page = page;
  130. bvec[0].bv_offset = 0;
  131. bvec[0].bv_len = PAGE_SIZE;
  132. iov_iter_bvec(&iter, ITER_DEST, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
  133. ret = fscache_begin_read_operation(&cres, cookie);
  134. if (ret < 0)
  135. return ret;
  136. ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
  137. NULL, NULL);
  138. fscache_end_operation(&cres);
  139. return ret;
  140. }
  141. /*
  142. * Fallback page writing interface.
  143. */
  144. static int fscache_fallback_write_page(struct inode *inode, struct page *page,
  145. bool no_space_allocated_yet)
  146. {
  147. struct netfs_cache_resources cres;
  148. struct fscache_cookie *cookie = cifs_inode_cookie(inode);
  149. struct iov_iter iter;
  150. struct bio_vec bvec[1];
  151. loff_t start = page_offset(page);
  152. size_t len = PAGE_SIZE;
  153. int ret;
  154. memset(&cres, 0, sizeof(cres));
  155. bvec[0].bv_page = page;
  156. bvec[0].bv_offset = 0;
  157. bvec[0].bv_len = PAGE_SIZE;
  158. iov_iter_bvec(&iter, ITER_SOURCE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
  159. ret = fscache_begin_write_operation(&cres, cookie);
  160. if (ret < 0)
  161. return ret;
  162. ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
  163. no_space_allocated_yet);
  164. if (ret == 0)
  165. ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
  166. fscache_end_operation(&cres);
  167. return ret;
  168. }
  169. /*
  170. * Retrieve a page from FS-Cache
  171. */
  172. int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
  173. {
  174. int ret;
  175. cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
  176. __func__, cifs_inode_cookie(inode), page, inode);
  177. ret = fscache_fallback_read_page(inode, page);
  178. if (ret < 0)
  179. return ret;
  180. /* Read completed synchronously */
  181. SetPageUptodate(page);
  182. return 0;
  183. }
  184. void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
  185. {
  186. cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
  187. __func__, cifs_inode_cookie(inode), page, inode);
  188. fscache_fallback_write_page(inode, page, true);
  189. }
  190. /*
  191. * Query the cache occupancy.
  192. */
  193. int __cifs_fscache_query_occupancy(struct inode *inode,
  194. pgoff_t first, unsigned int nr_pages,
  195. pgoff_t *_data_first,
  196. unsigned int *_data_nr_pages)
  197. {
  198. struct netfs_cache_resources cres;
  199. struct fscache_cookie *cookie = cifs_inode_cookie(inode);
  200. loff_t start, data_start;
  201. size_t len, data_len;
  202. int ret;
  203. ret = fscache_begin_read_operation(&cres, cookie);
  204. if (ret < 0)
  205. return ret;
  206. start = first * PAGE_SIZE;
  207. len = nr_pages * PAGE_SIZE;
  208. ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
  209. &data_start, &data_len);
  210. if (ret == 0) {
  211. *_data_first = data_start / PAGE_SIZE;
  212. *_data_nr_pages = len / PAGE_SIZE;
  213. }
  214. fscache_end_operation(&cres);
  215. return ret;
  216. }