file.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/fs.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/writeback.h>
  17. #include <linux/gfp.h>
  18. #include "internal.h"
  19. static int afs_readpage(struct file *file, struct page *page);
  20. static void afs_invalidatepage(struct page *page, unsigned int offset,
  21. unsigned int length);
  22. static int afs_releasepage(struct page *page, gfp_t gfp_flags);
  23. static int afs_launder_page(struct page *page);
  24. static int afs_readpages(struct file *filp, struct address_space *mapping,
  25. struct list_head *pages, unsigned nr_pages);
  26. const struct file_operations afs_file_operations = {
  27. .open = afs_open,
  28. .release = afs_release,
  29. .llseek = generic_file_llseek,
  30. .read_iter = generic_file_read_iter,
  31. .write_iter = afs_file_write,
  32. .mmap = generic_file_readonly_mmap,
  33. .splice_read = generic_file_splice_read,
  34. .fsync = afs_fsync,
  35. .lock = afs_lock,
  36. .flock = afs_flock,
  37. };
  38. const struct inode_operations afs_file_inode_operations = {
  39. .getattr = afs_getattr,
  40. .setattr = afs_setattr,
  41. .permission = afs_permission,
  42. };
  43. const struct address_space_operations afs_fs_aops = {
  44. .readpage = afs_readpage,
  45. .readpages = afs_readpages,
  46. .set_page_dirty = afs_set_page_dirty,
  47. .launder_page = afs_launder_page,
  48. .releasepage = afs_releasepage,
  49. .invalidatepage = afs_invalidatepage,
  50. .write_begin = afs_write_begin,
  51. .write_end = afs_write_end,
  52. .writepage = afs_writepage,
  53. .writepages = afs_writepages,
  54. };
  55. /*
  56. * open an AFS file or directory and attach a key to it
  57. */
  58. int afs_open(struct inode *inode, struct file *file)
  59. {
  60. struct afs_vnode *vnode = AFS_FS_I(inode);
  61. struct key *key;
  62. int ret;
  63. _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
  64. key = afs_request_key(vnode->volume->cell);
  65. if (IS_ERR(key)) {
  66. _leave(" = %ld [key]", PTR_ERR(key));
  67. return PTR_ERR(key);
  68. }
  69. ret = afs_validate(vnode, key);
  70. if (ret < 0) {
  71. _leave(" = %d [val]", ret);
  72. return ret;
  73. }
  74. file->private_data = key;
  75. _leave(" = 0");
  76. return 0;
  77. }
  78. /*
  79. * release an AFS file or directory and discard its key
  80. */
  81. int afs_release(struct inode *inode, struct file *file)
  82. {
  83. struct afs_vnode *vnode = AFS_FS_I(inode);
  84. _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
  85. key_put(file->private_data);
  86. _leave(" = 0");
  87. return 0;
  88. }
  89. #ifdef CONFIG_AFS_FSCACHE
  90. /*
  91. * deal with notification that a page was read from the cache
  92. */
  93. static void afs_file_readpage_read_complete(struct page *page,
  94. void *data,
  95. int error)
  96. {
  97. _enter("%p,%p,%d", page, data, error);
  98. /* if the read completes with an error, we just unlock the page and let
  99. * the VM reissue the readpage */
  100. if (!error)
  101. SetPageUptodate(page);
  102. unlock_page(page);
  103. }
  104. #endif
  105. /*
  106. * read page from file, directory or symlink, given a key to use
  107. */
  108. int afs_page_filler(void *data, struct page *page)
  109. {
  110. struct inode *inode = page->mapping->host;
  111. struct afs_vnode *vnode = AFS_FS_I(inode);
  112. struct key *key = data;
  113. size_t len;
  114. off_t offset;
  115. int ret;
  116. _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
  117. BUG_ON(!PageLocked(page));
  118. ret = -ESTALE;
  119. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  120. goto error;
  121. /* is it cached? */
  122. #ifdef CONFIG_AFS_FSCACHE
  123. ret = fscache_read_or_alloc_page(vnode->cache,
  124. page,
  125. afs_file_readpage_read_complete,
  126. NULL,
  127. GFP_KERNEL);
  128. #else
  129. ret = -ENOBUFS;
  130. #endif
  131. switch (ret) {
  132. /* read BIO submitted (page in cache) */
  133. case 0:
  134. break;
  135. /* page not yet cached */
  136. case -ENODATA:
  137. _debug("cache said ENODATA");
  138. goto go_on;
  139. /* page will not be cached */
  140. case -ENOBUFS:
  141. _debug("cache said ENOBUFS");
  142. default:
  143. go_on:
  144. offset = page->index << PAGE_CACHE_SHIFT;
  145. len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
  146. /* read the contents of the file from the server into the
  147. * page */
  148. ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
  149. if (ret < 0) {
  150. if (ret == -ENOENT) {
  151. _debug("got NOENT from server"
  152. " - marking file deleted and stale");
  153. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  154. ret = -ESTALE;
  155. }
  156. #ifdef CONFIG_AFS_FSCACHE
  157. fscache_uncache_page(vnode->cache, page);
  158. #endif
  159. BUG_ON(PageFsCache(page));
  160. goto error;
  161. }
  162. SetPageUptodate(page);
  163. /* send the page to the cache */
  164. #ifdef CONFIG_AFS_FSCACHE
  165. if (PageFsCache(page) &&
  166. fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
  167. fscache_uncache_page(vnode->cache, page);
  168. BUG_ON(PageFsCache(page));
  169. }
  170. #endif
  171. unlock_page(page);
  172. }
  173. _leave(" = 0");
  174. return 0;
  175. error:
  176. SetPageError(page);
  177. unlock_page(page);
  178. _leave(" = %d", ret);
  179. return ret;
  180. }
  181. /*
  182. * read page from file, directory or symlink, given a file to nominate the key
  183. * to be used
  184. */
  185. static int afs_readpage(struct file *file, struct page *page)
  186. {
  187. struct key *key;
  188. int ret;
  189. if (file) {
  190. key = file->private_data;
  191. ASSERT(key != NULL);
  192. ret = afs_page_filler(key, page);
  193. } else {
  194. struct inode *inode = page->mapping->host;
  195. key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
  196. if (IS_ERR(key)) {
  197. ret = PTR_ERR(key);
  198. } else {
  199. ret = afs_page_filler(key, page);
  200. key_put(key);
  201. }
  202. }
  203. return ret;
  204. }
  205. /*
  206. * read a set of pages
  207. */
  208. static int afs_readpages(struct file *file, struct address_space *mapping,
  209. struct list_head *pages, unsigned nr_pages)
  210. {
  211. struct key *key = file->private_data;
  212. struct afs_vnode *vnode;
  213. int ret = 0;
  214. _enter("{%d},{%lu},,%d",
  215. key_serial(key), mapping->host->i_ino, nr_pages);
  216. ASSERT(key != NULL);
  217. vnode = AFS_FS_I(mapping->host);
  218. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  219. _leave(" = -ESTALE");
  220. return -ESTALE;
  221. }
  222. /* attempt to read as many of the pages as possible */
  223. #ifdef CONFIG_AFS_FSCACHE
  224. ret = fscache_read_or_alloc_pages(vnode->cache,
  225. mapping,
  226. pages,
  227. &nr_pages,
  228. afs_file_readpage_read_complete,
  229. NULL,
  230. mapping_gfp_mask(mapping));
  231. #else
  232. ret = -ENOBUFS;
  233. #endif
  234. switch (ret) {
  235. /* all pages are being read from the cache */
  236. case 0:
  237. BUG_ON(!list_empty(pages));
  238. BUG_ON(nr_pages != 0);
  239. _leave(" = 0 [reading all]");
  240. return 0;
  241. /* there were pages that couldn't be read from the cache */
  242. case -ENODATA:
  243. case -ENOBUFS:
  244. break;
  245. /* other error */
  246. default:
  247. _leave(" = %d", ret);
  248. return ret;
  249. }
  250. /* load the missing pages from the network */
  251. ret = read_cache_pages(mapping, pages, afs_page_filler, key);
  252. _leave(" = %d [netting]", ret);
  253. return ret;
  254. }
  255. /*
  256. * write back a dirty page
  257. */
  258. static int afs_launder_page(struct page *page)
  259. {
  260. _enter("{%lu}", page->index);
  261. return 0;
  262. }
  263. /*
  264. * invalidate part or all of a page
  265. * - release a page and clean up its private data if offset is 0 (indicating
  266. * the entire page)
  267. */
  268. static void afs_invalidatepage(struct page *page, unsigned int offset,
  269. unsigned int length)
  270. {
  271. struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
  272. _enter("{%lu},%u,%u", page->index, offset, length);
  273. BUG_ON(!PageLocked(page));
  274. /* we clean up only if the entire page is being invalidated */
  275. if (offset == 0 && length == PAGE_CACHE_SIZE) {
  276. #ifdef CONFIG_AFS_FSCACHE
  277. if (PageFsCache(page)) {
  278. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  279. fscache_wait_on_page_write(vnode->cache, page);
  280. fscache_uncache_page(vnode->cache, page);
  281. }
  282. #endif
  283. if (PagePrivate(page)) {
  284. if (wb && !PageWriteback(page)) {
  285. set_page_private(page, 0);
  286. afs_put_writeback(wb);
  287. }
  288. if (!page_private(page))
  289. ClearPagePrivate(page);
  290. }
  291. }
  292. _leave("");
  293. }
  294. /*
  295. * release a page and clean up its private state if it's not busy
  296. * - return true if the page can now be released, false if not
  297. */
  298. static int afs_releasepage(struct page *page, gfp_t gfp_flags)
  299. {
  300. struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
  301. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  302. _enter("{{%x:%u}[%lu],%lx},%x",
  303. vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
  304. gfp_flags);
  305. /* deny if page is being written to the cache and the caller hasn't
  306. * elected to wait */
  307. #ifdef CONFIG_AFS_FSCACHE
  308. if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
  309. _leave(" = F [cache busy]");
  310. return 0;
  311. }
  312. #endif
  313. if (PagePrivate(page)) {
  314. if (wb) {
  315. set_page_private(page, 0);
  316. afs_put_writeback(wb);
  317. }
  318. ClearPagePrivate(page);
  319. }
  320. /* indicate that the page can be released */
  321. _leave(" = T");
  322. return 1;
  323. }