file.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* AFS filesystem file handling
  2. *
  3. * Copyright (C) 2002, 2007 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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include "internal.h"
  18. #if 0
  19. static int afs_file_open(struct inode *inode, struct file *file);
  20. static int afs_file_release(struct inode *inode, struct file *file);
  21. #endif
  22. static int afs_file_readpage(struct file *file, struct page *page);
  23. static void afs_file_invalidatepage(struct page *page, unsigned long offset);
  24. static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
  25. const struct inode_operations afs_file_inode_operations = {
  26. .getattr = afs_inode_getattr,
  27. };
  28. const struct address_space_operations afs_fs_aops = {
  29. .readpage = afs_file_readpage,
  30. .set_page_dirty = __set_page_dirty_nobuffers,
  31. .releasepage = afs_file_releasepage,
  32. .invalidatepage = afs_file_invalidatepage,
  33. };
  34. /*
  35. * deal with notification that a page was read from the cache
  36. */
  37. #ifdef AFS_CACHING_SUPPORT
  38. static void afs_file_readpage_read_complete(void *cookie_data,
  39. struct page *page,
  40. void *data,
  41. int error)
  42. {
  43. _enter("%p,%p,%p,%d", cookie_data, page, data, error);
  44. if (error)
  45. SetPageError(page);
  46. else
  47. SetPageUptodate(page);
  48. unlock_page(page);
  49. }
  50. #endif
  51. /*
  52. * deal with notification that a page was written to the cache
  53. */
  54. #ifdef AFS_CACHING_SUPPORT
  55. static void afs_file_readpage_write_complete(void *cookie_data,
  56. struct page *page,
  57. void *data,
  58. int error)
  59. {
  60. _enter("%p,%p,%p,%d", cookie_data, page, data, error);
  61. unlock_page(page);
  62. }
  63. #endif
  64. /*
  65. * AFS read page from file (or symlink)
  66. */
  67. static int afs_file_readpage(struct file *file, struct page *page)
  68. {
  69. struct afs_vnode *vnode;
  70. struct inode *inode;
  71. size_t len;
  72. off_t offset;
  73. int ret;
  74. inode = page->mapping->host;
  75. _enter("{%lu},{%lu}", inode->i_ino, page->index);
  76. vnode = AFS_FS_I(inode);
  77. BUG_ON(!PageLocked(page));
  78. ret = -ESTALE;
  79. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  80. goto error;
  81. #ifdef AFS_CACHING_SUPPORT
  82. /* is it cached? */
  83. ret = cachefs_read_or_alloc_page(vnode->cache,
  84. page,
  85. afs_file_readpage_read_complete,
  86. NULL,
  87. GFP_KERNEL);
  88. #else
  89. ret = -ENOBUFS;
  90. #endif
  91. switch (ret) {
  92. /* read BIO submitted and wb-journal entry found */
  93. case 1:
  94. BUG(); // TODO - handle wb-journal match
  95. /* read BIO submitted (page in cache) */
  96. case 0:
  97. break;
  98. /* no page available in cache */
  99. case -ENOBUFS:
  100. case -ENODATA:
  101. default:
  102. offset = page->index << PAGE_CACHE_SHIFT;
  103. len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
  104. /* read the contents of the file from the server into the
  105. * page */
  106. ret = afs_vnode_fetch_data(vnode, offset, len, page);
  107. if (ret < 0) {
  108. if (ret == -ENOENT) {
  109. _debug("got NOENT from server"
  110. " - marking file deleted and stale");
  111. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  112. ret = -ESTALE;
  113. }
  114. #ifdef AFS_CACHING_SUPPORT
  115. cachefs_uncache_page(vnode->cache, page);
  116. #endif
  117. goto error;
  118. }
  119. SetPageUptodate(page);
  120. #ifdef AFS_CACHING_SUPPORT
  121. if (cachefs_write_page(vnode->cache,
  122. page,
  123. afs_file_readpage_write_complete,
  124. NULL,
  125. GFP_KERNEL) != 0
  126. ) {
  127. cachefs_uncache_page(vnode->cache, page);
  128. unlock_page(page);
  129. }
  130. #else
  131. unlock_page(page);
  132. #endif
  133. }
  134. _leave(" = 0");
  135. return 0;
  136. error:
  137. SetPageError(page);
  138. unlock_page(page);
  139. _leave(" = %d", ret);
  140. return ret;
  141. }
  142. /*
  143. * get a page cookie for the specified page
  144. */
  145. #ifdef AFS_CACHING_SUPPORT
  146. int afs_cache_get_page_cookie(struct page *page,
  147. struct cachefs_page **_page_cookie)
  148. {
  149. int ret;
  150. _enter("");
  151. ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
  152. _leave(" = %d", ret);
  153. return ret;
  154. }
  155. #endif
  156. /*
  157. * invalidate part or all of a page
  158. */
  159. static void afs_file_invalidatepage(struct page *page, unsigned long offset)
  160. {
  161. int ret = 1;
  162. _enter("{%lu},%lu", page->index, offset);
  163. BUG_ON(!PageLocked(page));
  164. if (PagePrivate(page)) {
  165. #ifdef AFS_CACHING_SUPPORT
  166. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  167. cachefs_uncache_page(vnode->cache,page);
  168. #endif
  169. /* We release buffers only if the entire page is being
  170. * invalidated.
  171. * The get_block cached value has been unconditionally
  172. * invalidated, so real IO is not possible anymore.
  173. */
  174. if (offset == 0) {
  175. BUG_ON(!PageLocked(page));
  176. ret = 0;
  177. if (!PageWriteback(page))
  178. ret = page->mapping->a_ops->releasepage(page,
  179. 0);
  180. /* possibly should BUG_ON(!ret); - neilb */
  181. }
  182. }
  183. _leave(" = %d", ret);
  184. }
  185. /*
  186. * release a page and cleanup its private data
  187. */
  188. static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
  189. {
  190. struct cachefs_page *pageio;
  191. _enter("{%lu},%x", page->index, gfp_flags);
  192. if (PagePrivate(page)) {
  193. #ifdef AFS_CACHING_SUPPORT
  194. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  195. cachefs_uncache_page(vnode->cache, page);
  196. #endif
  197. pageio = (struct cachefs_page *) page_private(page);
  198. set_page_private(page, 0);
  199. ClearPagePrivate(page);
  200. kfree(pageio);
  201. }
  202. _leave(" = 0");
  203. return 0;
  204. }