file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 <linux/task_io_accounting_ops.h>
  19. #include "internal.h"
  20. static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
  21. static int afs_readpage(struct file *file, struct page *page);
  22. static void afs_invalidatepage(struct page *page, unsigned int offset,
  23. unsigned int length);
  24. static int afs_releasepage(struct page *page, gfp_t gfp_flags);
  25. static int afs_readpages(struct file *filp, struct address_space *mapping,
  26. struct list_head *pages, unsigned nr_pages);
  27. const struct file_operations afs_file_operations = {
  28. .open = afs_open,
  29. .release = afs_release,
  30. .llseek = generic_file_llseek,
  31. .read_iter = generic_file_read_iter,
  32. .write_iter = afs_file_write,
  33. .mmap = afs_file_mmap,
  34. .splice_read = generic_file_splice_read,
  35. .fsync = afs_fsync,
  36. .lock = afs_lock,
  37. .flock = afs_flock,
  38. };
  39. const struct inode_operations afs_file_inode_operations = {
  40. .getattr = afs_getattr,
  41. .setattr = afs_setattr,
  42. .permission = afs_permission,
  43. .listxattr = afs_listxattr,
  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. static const struct vm_operations_struct afs_vm_ops = {
  58. .fault = filemap_fault,
  59. .map_pages = filemap_map_pages,
  60. .page_mkwrite = afs_page_mkwrite,
  61. };
  62. /*
  63. * Discard a pin on a writeback key.
  64. */
  65. void afs_put_wb_key(struct afs_wb_key *wbk)
  66. {
  67. if (refcount_dec_and_test(&wbk->usage)) {
  68. key_put(wbk->key);
  69. kfree(wbk);
  70. }
  71. }
  72. /*
  73. * Cache key for writeback.
  74. */
  75. int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
  76. {
  77. struct afs_wb_key *wbk, *p;
  78. wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
  79. if (!wbk)
  80. return -ENOMEM;
  81. refcount_set(&wbk->usage, 2);
  82. wbk->key = af->key;
  83. spin_lock(&vnode->wb_lock);
  84. list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
  85. if (p->key == wbk->key)
  86. goto found;
  87. }
  88. key_get(wbk->key);
  89. list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
  90. spin_unlock(&vnode->wb_lock);
  91. af->wb = wbk;
  92. return 0;
  93. found:
  94. refcount_inc(&p->usage);
  95. spin_unlock(&vnode->wb_lock);
  96. af->wb = p;
  97. kfree(wbk);
  98. return 0;
  99. }
  100. /*
  101. * open an AFS file or directory and attach a key to it
  102. */
  103. int afs_open(struct inode *inode, struct file *file)
  104. {
  105. struct afs_vnode *vnode = AFS_FS_I(inode);
  106. struct afs_file *af;
  107. struct key *key;
  108. int ret;
  109. _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
  110. key = afs_request_key(vnode->volume->cell);
  111. if (IS_ERR(key)) {
  112. ret = PTR_ERR(key);
  113. goto error;
  114. }
  115. af = kzalloc(sizeof(*af), GFP_KERNEL);
  116. if (!af) {
  117. ret = -ENOMEM;
  118. goto error_key;
  119. }
  120. af->key = key;
  121. ret = afs_validate(vnode, key);
  122. if (ret < 0)
  123. goto error_af;
  124. if (file->f_mode & FMODE_WRITE) {
  125. ret = afs_cache_wb_key(vnode, af);
  126. if (ret < 0)
  127. goto error_af;
  128. }
  129. if (file->f_flags & O_TRUNC)
  130. set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  131. file->private_data = af;
  132. _leave(" = 0");
  133. return 0;
  134. error_af:
  135. kfree(af);
  136. error_key:
  137. key_put(key);
  138. error:
  139. _leave(" = %d", ret);
  140. return ret;
  141. }
  142. /*
  143. * release an AFS file or directory and discard its key
  144. */
  145. int afs_release(struct inode *inode, struct file *file)
  146. {
  147. struct afs_vnode *vnode = AFS_FS_I(inode);
  148. struct afs_file *af = file->private_data;
  149. int ret = 0;
  150. _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
  151. if ((file->f_mode & FMODE_WRITE))
  152. ret = vfs_fsync(file, 0);
  153. file->private_data = NULL;
  154. if (af->wb)
  155. afs_put_wb_key(af->wb);
  156. key_put(af->key);
  157. kfree(af);
  158. afs_prune_wb_keys(vnode);
  159. _leave(" = %d", ret);
  160. return ret;
  161. }
  162. /*
  163. * Dispose of a ref to a read record.
  164. */
  165. void afs_put_read(struct afs_read *req)
  166. {
  167. int i;
  168. if (refcount_dec_and_test(&req->usage)) {
  169. if (req->pages) {
  170. for (i = 0; i < req->nr_pages; i++)
  171. if (req->pages[i])
  172. put_page(req->pages[i]);
  173. if (req->pages != req->array)
  174. kfree(req->pages);
  175. }
  176. kfree(req);
  177. }
  178. }
  179. #ifdef CONFIG_AFS_FSCACHE
  180. /*
  181. * deal with notification that a page was read from the cache
  182. */
  183. static void afs_file_readpage_read_complete(struct page *page,
  184. void *data,
  185. int error)
  186. {
  187. _enter("%p,%p,%d", page, data, error);
  188. /* if the read completes with an error, we just unlock the page and let
  189. * the VM reissue the readpage */
  190. if (!error)
  191. SetPageUptodate(page);
  192. unlock_page(page);
  193. }
  194. #endif
  195. /*
  196. * Fetch file data from the volume.
  197. */
  198. int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
  199. {
  200. struct afs_fs_cursor fc;
  201. int ret;
  202. _enter("%s{%x:%u.%u},%x,,,",
  203. vnode->volume->name,
  204. vnode->fid.vid,
  205. vnode->fid.vnode,
  206. vnode->fid.unique,
  207. key_serial(key));
  208. ret = -ERESTARTSYS;
  209. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  210. while (afs_select_fileserver(&fc)) {
  211. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  212. afs_fs_fetch_data(&fc, desc);
  213. }
  214. afs_check_for_remote_deletion(&fc, fc.vnode);
  215. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  216. ret = afs_end_vnode_operation(&fc);
  217. }
  218. if (ret == 0) {
  219. afs_stat_v(vnode, n_fetches);
  220. atomic_long_add(desc->actual_len,
  221. &afs_v2net(vnode)->n_fetch_bytes);
  222. }
  223. _leave(" = %d", ret);
  224. return ret;
  225. }
  226. /*
  227. * read page from file, directory or symlink, given a key to use
  228. */
  229. int afs_page_filler(void *data, struct page *page)
  230. {
  231. struct inode *inode = page->mapping->host;
  232. struct afs_vnode *vnode = AFS_FS_I(inode);
  233. struct afs_read *req;
  234. struct key *key = data;
  235. int ret;
  236. _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
  237. BUG_ON(!PageLocked(page));
  238. ret = -ESTALE;
  239. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  240. goto error;
  241. /* is it cached? */
  242. #ifdef CONFIG_AFS_FSCACHE
  243. ret = fscache_read_or_alloc_page(vnode->cache,
  244. page,
  245. afs_file_readpage_read_complete,
  246. NULL,
  247. GFP_KERNEL);
  248. #else
  249. ret = -ENOBUFS;
  250. #endif
  251. switch (ret) {
  252. /* read BIO submitted (page in cache) */
  253. case 0:
  254. break;
  255. /* page not yet cached */
  256. case -ENODATA:
  257. _debug("cache said ENODATA");
  258. goto go_on;
  259. /* page will not be cached */
  260. case -ENOBUFS:
  261. _debug("cache said ENOBUFS");
  262. default:
  263. go_on:
  264. req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
  265. GFP_KERNEL);
  266. if (!req)
  267. goto enomem;
  268. /* We request a full page. If the page is a partial one at the
  269. * end of the file, the server will return a short read and the
  270. * unmarshalling code will clear the unfilled space.
  271. */
  272. refcount_set(&req->usage, 1);
  273. req->pos = (loff_t)page->index << PAGE_SHIFT;
  274. req->len = PAGE_SIZE;
  275. req->nr_pages = 1;
  276. req->pages = req->array;
  277. req->pages[0] = page;
  278. get_page(page);
  279. /* read the contents of the file from the server into the
  280. * page */
  281. ret = afs_fetch_data(vnode, key, req);
  282. afs_put_read(req);
  283. if (ret < 0) {
  284. if (ret == -ENOENT) {
  285. _debug("got NOENT from server"
  286. " - marking file deleted and stale");
  287. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  288. ret = -ESTALE;
  289. }
  290. #ifdef CONFIG_AFS_FSCACHE
  291. fscache_uncache_page(vnode->cache, page);
  292. #endif
  293. BUG_ON(PageFsCache(page));
  294. if (ret == -EINTR ||
  295. ret == -ENOMEM ||
  296. ret == -ERESTARTSYS ||
  297. ret == -EAGAIN)
  298. goto error;
  299. goto io_error;
  300. }
  301. SetPageUptodate(page);
  302. /* send the page to the cache */
  303. #ifdef CONFIG_AFS_FSCACHE
  304. if (PageFsCache(page) &&
  305. fscache_write_page(vnode->cache, page, vnode->status.size,
  306. GFP_KERNEL) != 0) {
  307. fscache_uncache_page(vnode->cache, page);
  308. BUG_ON(PageFsCache(page));
  309. }
  310. #endif
  311. unlock_page(page);
  312. }
  313. _leave(" = 0");
  314. return 0;
  315. io_error:
  316. SetPageError(page);
  317. goto error;
  318. enomem:
  319. ret = -ENOMEM;
  320. error:
  321. unlock_page(page);
  322. _leave(" = %d", ret);
  323. return ret;
  324. }
  325. /*
  326. * read page from file, directory or symlink, given a file to nominate the key
  327. * to be used
  328. */
  329. static int afs_readpage(struct file *file, struct page *page)
  330. {
  331. struct key *key;
  332. int ret;
  333. if (file) {
  334. key = afs_file_key(file);
  335. ASSERT(key != NULL);
  336. ret = afs_page_filler(key, page);
  337. } else {
  338. struct inode *inode = page->mapping->host;
  339. key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
  340. if (IS_ERR(key)) {
  341. ret = PTR_ERR(key);
  342. } else {
  343. ret = afs_page_filler(key, page);
  344. key_put(key);
  345. }
  346. }
  347. return ret;
  348. }
  349. /*
  350. * Make pages available as they're filled.
  351. */
  352. static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
  353. {
  354. #ifdef CONFIG_AFS_FSCACHE
  355. struct afs_vnode *vnode = call->reply[0];
  356. #endif
  357. struct page *page = req->pages[req->index];
  358. req->pages[req->index] = NULL;
  359. SetPageUptodate(page);
  360. /* send the page to the cache */
  361. #ifdef CONFIG_AFS_FSCACHE
  362. if (PageFsCache(page) &&
  363. fscache_write_page(vnode->cache, page, vnode->status.size,
  364. GFP_KERNEL) != 0) {
  365. fscache_uncache_page(vnode->cache, page);
  366. BUG_ON(PageFsCache(page));
  367. }
  368. #endif
  369. unlock_page(page);
  370. put_page(page);
  371. }
  372. /*
  373. * Read a contiguous set of pages.
  374. */
  375. static int afs_readpages_one(struct file *file, struct address_space *mapping,
  376. struct list_head *pages)
  377. {
  378. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  379. struct afs_read *req;
  380. struct list_head *p;
  381. struct page *first, *page;
  382. struct key *key = afs_file_key(file);
  383. pgoff_t index;
  384. int ret, n, i;
  385. /* Count the number of contiguous pages at the front of the list. Note
  386. * that the list goes prev-wards rather than next-wards.
  387. */
  388. first = list_entry(pages->prev, struct page, lru);
  389. index = first->index + 1;
  390. n = 1;
  391. for (p = first->lru.prev; p != pages; p = p->prev) {
  392. page = list_entry(p, struct page, lru);
  393. if (page->index != index)
  394. break;
  395. index++;
  396. n++;
  397. }
  398. req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
  399. GFP_NOFS);
  400. if (!req)
  401. return -ENOMEM;
  402. refcount_set(&req->usage, 1);
  403. req->page_done = afs_readpages_page_done;
  404. req->pos = first->index;
  405. req->pos <<= PAGE_SHIFT;
  406. req->pages = req->array;
  407. /* Transfer the pages to the request. We add them in until one fails
  408. * to add to the LRU and then we stop (as that'll make a hole in the
  409. * contiguous run.
  410. *
  411. * Note that it's possible for the file size to change whilst we're
  412. * doing this, but we rely on the server returning less than we asked
  413. * for if the file shrank. We also rely on this to deal with a partial
  414. * page at the end of the file.
  415. */
  416. do {
  417. page = list_entry(pages->prev, struct page, lru);
  418. list_del(&page->lru);
  419. index = page->index;
  420. if (add_to_page_cache_lru(page, mapping, index,
  421. readahead_gfp_mask(mapping))) {
  422. #ifdef CONFIG_AFS_FSCACHE
  423. fscache_uncache_page(vnode->cache, page);
  424. #endif
  425. put_page(page);
  426. break;
  427. }
  428. req->pages[req->nr_pages++] = page;
  429. req->len += PAGE_SIZE;
  430. } while (req->nr_pages < n);
  431. if (req->nr_pages == 0) {
  432. kfree(req);
  433. return 0;
  434. }
  435. ret = afs_fetch_data(vnode, key, req);
  436. if (ret < 0)
  437. goto error;
  438. task_io_account_read(PAGE_SIZE * req->nr_pages);
  439. afs_put_read(req);
  440. return 0;
  441. error:
  442. if (ret == -ENOENT) {
  443. _debug("got NOENT from server"
  444. " - marking file deleted and stale");
  445. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  446. ret = -ESTALE;
  447. }
  448. for (i = 0; i < req->nr_pages; i++) {
  449. page = req->pages[i];
  450. if (page) {
  451. #ifdef CONFIG_AFS_FSCACHE
  452. fscache_uncache_page(vnode->cache, page);
  453. #endif
  454. SetPageError(page);
  455. unlock_page(page);
  456. }
  457. }
  458. afs_put_read(req);
  459. return ret;
  460. }
  461. /*
  462. * read a set of pages
  463. */
  464. static int afs_readpages(struct file *file, struct address_space *mapping,
  465. struct list_head *pages, unsigned nr_pages)
  466. {
  467. struct key *key = afs_file_key(file);
  468. struct afs_vnode *vnode;
  469. int ret = 0;
  470. _enter("{%d},{%lu},,%d",
  471. key_serial(key), mapping->host->i_ino, nr_pages);
  472. ASSERT(key != NULL);
  473. vnode = AFS_FS_I(mapping->host);
  474. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  475. _leave(" = -ESTALE");
  476. return -ESTALE;
  477. }
  478. /* attempt to read as many of the pages as possible */
  479. #ifdef CONFIG_AFS_FSCACHE
  480. ret = fscache_read_or_alloc_pages(vnode->cache,
  481. mapping,
  482. pages,
  483. &nr_pages,
  484. afs_file_readpage_read_complete,
  485. NULL,
  486. mapping_gfp_mask(mapping));
  487. #else
  488. ret = -ENOBUFS;
  489. #endif
  490. switch (ret) {
  491. /* all pages are being read from the cache */
  492. case 0:
  493. BUG_ON(!list_empty(pages));
  494. BUG_ON(nr_pages != 0);
  495. _leave(" = 0 [reading all]");
  496. return 0;
  497. /* there were pages that couldn't be read from the cache */
  498. case -ENODATA:
  499. case -ENOBUFS:
  500. break;
  501. /* other error */
  502. default:
  503. _leave(" = %d", ret);
  504. return ret;
  505. }
  506. while (!list_empty(pages)) {
  507. ret = afs_readpages_one(file, mapping, pages);
  508. if (ret < 0)
  509. break;
  510. }
  511. _leave(" = %d [netting]", ret);
  512. return ret;
  513. }
  514. /*
  515. * invalidate part or all of a page
  516. * - release a page and clean up its private data if offset is 0 (indicating
  517. * the entire page)
  518. */
  519. static void afs_invalidatepage(struct page *page, unsigned int offset,
  520. unsigned int length)
  521. {
  522. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  523. unsigned long priv;
  524. _enter("{%lu},%u,%u", page->index, offset, length);
  525. BUG_ON(!PageLocked(page));
  526. /* we clean up only if the entire page is being invalidated */
  527. if (offset == 0 && length == PAGE_SIZE) {
  528. #ifdef CONFIG_AFS_FSCACHE
  529. if (PageFsCache(page)) {
  530. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  531. fscache_wait_on_page_write(vnode->cache, page);
  532. fscache_uncache_page(vnode->cache, page);
  533. }
  534. #endif
  535. if (PagePrivate(page)) {
  536. priv = page_private(page);
  537. trace_afs_page_dirty(vnode, tracepoint_string("inval"),
  538. page->index, priv);
  539. set_page_private(page, 0);
  540. ClearPagePrivate(page);
  541. }
  542. }
  543. _leave("");
  544. }
  545. /*
  546. * release a page and clean up its private state if it's not busy
  547. * - return true if the page can now be released, false if not
  548. */
  549. static int afs_releasepage(struct page *page, gfp_t gfp_flags)
  550. {
  551. struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
  552. unsigned long priv;
  553. _enter("{{%x:%u}[%lu],%lx},%x",
  554. vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
  555. gfp_flags);
  556. /* deny if page is being written to the cache and the caller hasn't
  557. * elected to wait */
  558. #ifdef CONFIG_AFS_FSCACHE
  559. if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
  560. _leave(" = F [cache busy]");
  561. return 0;
  562. }
  563. #endif
  564. if (PagePrivate(page)) {
  565. priv = page_private(page);
  566. trace_afs_page_dirty(vnode, tracepoint_string("rel"),
  567. page->index, priv);
  568. set_page_private(page, 0);
  569. ClearPagePrivate(page);
  570. }
  571. /* indicate that the page can be released */
  572. _leave(" = T");
  573. return 1;
  574. }
  575. /*
  576. * Handle setting up a memory mapping on an AFS file.
  577. */
  578. static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
  579. {
  580. int ret;
  581. ret = generic_file_mmap(file, vma);
  582. if (ret == 0)
  583. vma->vm_ops = &afs_vm_ops;
  584. return ret;
  585. }