file.c 8.7 KB

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