write.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /* handling of writes to regular files and writing back to the server
  2. *
  3. * Copyright (C) 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/backing-dev.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/writeback.h>
  16. #include <linux/pagevec.h>
  17. #include "internal.h"
  18. /*
  19. * mark a page as having been made dirty and thus needing writeback
  20. */
  21. int afs_set_page_dirty(struct page *page)
  22. {
  23. _enter("");
  24. return __set_page_dirty_nobuffers(page);
  25. }
  26. /*
  27. * partly or wholly fill a page that's under preparation for writing
  28. */
  29. static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
  30. loff_t pos, unsigned int len, struct page *page)
  31. {
  32. struct afs_read *req;
  33. size_t p;
  34. void *data;
  35. int ret;
  36. _enter(",,%llu", (unsigned long long)pos);
  37. if (pos >= vnode->vfs_inode.i_size) {
  38. p = pos & ~PAGE_MASK;
  39. ASSERTCMP(p + len, <=, PAGE_SIZE);
  40. data = kmap(page);
  41. memset(data + p, 0, len);
  42. kunmap(page);
  43. return 0;
  44. }
  45. req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
  46. GFP_KERNEL);
  47. if (!req)
  48. return -ENOMEM;
  49. refcount_set(&req->usage, 1);
  50. req->pos = pos;
  51. req->len = len;
  52. req->nr_pages = 1;
  53. req->pages = req->array;
  54. req->pages[0] = page;
  55. get_page(page);
  56. ret = afs_fetch_data(vnode, key, req);
  57. afs_put_read(req);
  58. if (ret < 0) {
  59. if (ret == -ENOENT) {
  60. _debug("got NOENT from server"
  61. " - marking file deleted and stale");
  62. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  63. ret = -ESTALE;
  64. }
  65. }
  66. _leave(" = %d", ret);
  67. return ret;
  68. }
  69. /*
  70. * prepare to perform part of a write to a page
  71. */
  72. int afs_write_begin(struct file *file, struct address_space *mapping,
  73. loff_t pos, unsigned len, unsigned flags,
  74. struct page **pagep, void **fsdata)
  75. {
  76. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  77. struct page *page;
  78. struct key *key = afs_file_key(file);
  79. unsigned long priv;
  80. unsigned f, from = pos & (PAGE_SIZE - 1);
  81. unsigned t, to = from + len;
  82. pgoff_t index = pos >> PAGE_SHIFT;
  83. int ret;
  84. _enter("{%llx:%llu},{%lx},%u,%u",
  85. vnode->fid.vid, vnode->fid.vnode, index, from, to);
  86. /* We want to store information about how much of a page is altered in
  87. * page->private.
  88. */
  89. BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
  90. page = grab_cache_page_write_begin(mapping, index, flags);
  91. if (!page)
  92. return -ENOMEM;
  93. if (!PageUptodate(page) && len != PAGE_SIZE) {
  94. ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
  95. if (ret < 0) {
  96. unlock_page(page);
  97. put_page(page);
  98. _leave(" = %d [prep]", ret);
  99. return ret;
  100. }
  101. SetPageUptodate(page);
  102. }
  103. /* page won't leak in error case: it eventually gets cleaned off LRU */
  104. *pagep = page;
  105. try_again:
  106. /* See if this page is already partially written in a way that we can
  107. * merge the new write with.
  108. */
  109. t = f = 0;
  110. if (PagePrivate(page)) {
  111. priv = page_private(page);
  112. f = priv & AFS_PRIV_MAX;
  113. t = priv >> AFS_PRIV_SHIFT;
  114. ASSERTCMP(f, <=, t);
  115. }
  116. if (f != t) {
  117. if (PageWriteback(page)) {
  118. trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
  119. page->index, priv);
  120. goto flush_conflicting_write;
  121. }
  122. /* If the file is being filled locally, allow inter-write
  123. * spaces to be merged into writes. If it's not, only write
  124. * back what the user gives us.
  125. */
  126. if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
  127. (to < f || from > t))
  128. goto flush_conflicting_write;
  129. if (from < f)
  130. f = from;
  131. if (to > t)
  132. t = to;
  133. } else {
  134. f = from;
  135. t = to;
  136. }
  137. priv = (unsigned long)t << AFS_PRIV_SHIFT;
  138. priv |= f;
  139. trace_afs_page_dirty(vnode, tracepoint_string("begin"),
  140. page->index, priv);
  141. SetPagePrivate(page);
  142. set_page_private(page, priv);
  143. _leave(" = 0");
  144. return 0;
  145. /* The previous write and this write aren't adjacent or overlapping, so
  146. * flush the page out.
  147. */
  148. flush_conflicting_write:
  149. _debug("flush conflict");
  150. ret = write_one_page(page);
  151. if (ret < 0) {
  152. _leave(" = %d", ret);
  153. return ret;
  154. }
  155. ret = lock_page_killable(page);
  156. if (ret < 0) {
  157. _leave(" = %d", ret);
  158. return ret;
  159. }
  160. goto try_again;
  161. }
  162. /*
  163. * finalise part of a write to a page
  164. */
  165. int afs_write_end(struct file *file, struct address_space *mapping,
  166. loff_t pos, unsigned len, unsigned copied,
  167. struct page *page, void *fsdata)
  168. {
  169. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  170. struct key *key = afs_file_key(file);
  171. loff_t i_size, maybe_i_size;
  172. int ret;
  173. _enter("{%llx:%llu},{%lx}",
  174. vnode->fid.vid, vnode->fid.vnode, page->index);
  175. maybe_i_size = pos + copied;
  176. i_size = i_size_read(&vnode->vfs_inode);
  177. if (maybe_i_size > i_size) {
  178. spin_lock(&vnode->wb_lock);
  179. i_size = i_size_read(&vnode->vfs_inode);
  180. if (maybe_i_size > i_size)
  181. i_size_write(&vnode->vfs_inode, maybe_i_size);
  182. spin_unlock(&vnode->wb_lock);
  183. }
  184. if (!PageUptodate(page)) {
  185. if (copied < len) {
  186. /* Try and load any missing data from the server. The
  187. * unmarshalling routine will take care of clearing any
  188. * bits that are beyond the EOF.
  189. */
  190. ret = afs_fill_page(vnode, key, pos + copied,
  191. len - copied, page);
  192. if (ret < 0)
  193. goto out;
  194. }
  195. SetPageUptodate(page);
  196. }
  197. set_page_dirty(page);
  198. if (PageDirty(page))
  199. _debug("dirtied");
  200. ret = copied;
  201. out:
  202. unlock_page(page);
  203. put_page(page);
  204. return ret;
  205. }
  206. /*
  207. * kill all the pages in the given range
  208. */
  209. static void afs_kill_pages(struct address_space *mapping,
  210. pgoff_t first, pgoff_t last)
  211. {
  212. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  213. struct pagevec pv;
  214. unsigned count, loop;
  215. _enter("{%llx:%llu},%lx-%lx",
  216. vnode->fid.vid, vnode->fid.vnode, first, last);
  217. pagevec_init(&pv);
  218. do {
  219. _debug("kill %lx-%lx", first, last);
  220. count = last - first + 1;
  221. if (count > PAGEVEC_SIZE)
  222. count = PAGEVEC_SIZE;
  223. pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
  224. ASSERTCMP(pv.nr, ==, count);
  225. for (loop = 0; loop < count; loop++) {
  226. struct page *page = pv.pages[loop];
  227. ClearPageUptodate(page);
  228. SetPageError(page);
  229. end_page_writeback(page);
  230. if (page->index >= first)
  231. first = page->index + 1;
  232. lock_page(page);
  233. generic_error_remove_page(mapping, page);
  234. }
  235. __pagevec_release(&pv);
  236. } while (first <= last);
  237. _leave("");
  238. }
  239. /*
  240. * Redirty all the pages in a given range.
  241. */
  242. static void afs_redirty_pages(struct writeback_control *wbc,
  243. struct address_space *mapping,
  244. pgoff_t first, pgoff_t last)
  245. {
  246. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  247. struct pagevec pv;
  248. unsigned count, loop;
  249. _enter("{%llx:%llu},%lx-%lx",
  250. vnode->fid.vid, vnode->fid.vnode, first, last);
  251. pagevec_init(&pv);
  252. do {
  253. _debug("redirty %lx-%lx", first, last);
  254. count = last - first + 1;
  255. if (count > PAGEVEC_SIZE)
  256. count = PAGEVEC_SIZE;
  257. pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
  258. ASSERTCMP(pv.nr, ==, count);
  259. for (loop = 0; loop < count; loop++) {
  260. struct page *page = pv.pages[loop];
  261. redirty_page_for_writepage(wbc, page);
  262. end_page_writeback(page);
  263. if (page->index >= first)
  264. first = page->index + 1;
  265. }
  266. __pagevec_release(&pv);
  267. } while (first <= last);
  268. _leave("");
  269. }
  270. /*
  271. * write to a file
  272. */
  273. static int afs_store_data(struct address_space *mapping,
  274. pgoff_t first, pgoff_t last,
  275. unsigned offset, unsigned to)
  276. {
  277. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  278. struct afs_fs_cursor fc;
  279. struct afs_wb_key *wbk = NULL;
  280. struct list_head *p;
  281. int ret = -ENOKEY, ret2;
  282. _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x",
  283. vnode->volume->name,
  284. vnode->fid.vid,
  285. vnode->fid.vnode,
  286. vnode->fid.unique,
  287. first, last, offset, to);
  288. spin_lock(&vnode->wb_lock);
  289. p = vnode->wb_keys.next;
  290. /* Iterate through the list looking for a valid key to use. */
  291. try_next_key:
  292. while (p != &vnode->wb_keys) {
  293. wbk = list_entry(p, struct afs_wb_key, vnode_link);
  294. _debug("wbk %u", key_serial(wbk->key));
  295. ret2 = key_validate(wbk->key);
  296. if (ret2 == 0)
  297. goto found_key;
  298. if (ret == -ENOKEY)
  299. ret = ret2;
  300. p = p->next;
  301. }
  302. spin_unlock(&vnode->wb_lock);
  303. afs_put_wb_key(wbk);
  304. _leave(" = %d [no keys]", ret);
  305. return ret;
  306. found_key:
  307. refcount_inc(&wbk->usage);
  308. spin_unlock(&vnode->wb_lock);
  309. _debug("USE WB KEY %u", key_serial(wbk->key));
  310. ret = -ERESTARTSYS;
  311. if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) {
  312. while (afs_select_fileserver(&fc)) {
  313. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  314. afs_fs_store_data(&fc, mapping, first, last, offset, to);
  315. }
  316. afs_check_for_remote_deletion(&fc, fc.vnode);
  317. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  318. ret = afs_end_vnode_operation(&fc);
  319. }
  320. switch (ret) {
  321. case 0:
  322. afs_stat_v(vnode, n_stores);
  323. atomic_long_add((last * PAGE_SIZE + to) -
  324. (first * PAGE_SIZE + offset),
  325. &afs_v2net(vnode)->n_store_bytes);
  326. break;
  327. case -EACCES:
  328. case -EPERM:
  329. case -ENOKEY:
  330. case -EKEYEXPIRED:
  331. case -EKEYREJECTED:
  332. case -EKEYREVOKED:
  333. _debug("next");
  334. spin_lock(&vnode->wb_lock);
  335. p = wbk->vnode_link.next;
  336. afs_put_wb_key(wbk);
  337. goto try_next_key;
  338. }
  339. afs_put_wb_key(wbk);
  340. _leave(" = %d", ret);
  341. return ret;
  342. }
  343. /*
  344. * Synchronously write back the locked page and any subsequent non-locked dirty
  345. * pages.
  346. */
  347. static int afs_write_back_from_locked_page(struct address_space *mapping,
  348. struct writeback_control *wbc,
  349. struct page *primary_page,
  350. pgoff_t final_page)
  351. {
  352. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  353. struct page *pages[8], *page;
  354. unsigned long count, priv;
  355. unsigned n, offset, to, f, t;
  356. pgoff_t start, first, last;
  357. int loop, ret;
  358. _enter(",%lx", primary_page->index);
  359. count = 1;
  360. if (test_set_page_writeback(primary_page))
  361. BUG();
  362. /* Find all consecutive lockable dirty pages that have contiguous
  363. * written regions, stopping when we find a page that is not
  364. * immediately lockable, is not dirty or is missing, or we reach the
  365. * end of the range.
  366. */
  367. start = primary_page->index;
  368. priv = page_private(primary_page);
  369. offset = priv & AFS_PRIV_MAX;
  370. to = priv >> AFS_PRIV_SHIFT;
  371. trace_afs_page_dirty(vnode, tracepoint_string("store"),
  372. primary_page->index, priv);
  373. WARN_ON(offset == to);
  374. if (offset == to)
  375. trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
  376. primary_page->index, priv);
  377. if (start >= final_page ||
  378. (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)))
  379. goto no_more;
  380. start++;
  381. do {
  382. _debug("more %lx [%lx]", start, count);
  383. n = final_page - start + 1;
  384. if (n > ARRAY_SIZE(pages))
  385. n = ARRAY_SIZE(pages);
  386. n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
  387. _debug("fgpc %u", n);
  388. if (n == 0)
  389. goto no_more;
  390. if (pages[0]->index != start) {
  391. do {
  392. put_page(pages[--n]);
  393. } while (n > 0);
  394. goto no_more;
  395. }
  396. for (loop = 0; loop < n; loop++) {
  397. page = pages[loop];
  398. if (to != PAGE_SIZE &&
  399. !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))
  400. break;
  401. if (page->index > final_page)
  402. break;
  403. if (!trylock_page(page))
  404. break;
  405. if (!PageDirty(page) || PageWriteback(page)) {
  406. unlock_page(page);
  407. break;
  408. }
  409. priv = page_private(page);
  410. f = priv & AFS_PRIV_MAX;
  411. t = priv >> AFS_PRIV_SHIFT;
  412. if (f != 0 &&
  413. !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) {
  414. unlock_page(page);
  415. break;
  416. }
  417. to = t;
  418. trace_afs_page_dirty(vnode, tracepoint_string("store+"),
  419. page->index, priv);
  420. if (!clear_page_dirty_for_io(page))
  421. BUG();
  422. if (test_set_page_writeback(page))
  423. BUG();
  424. unlock_page(page);
  425. put_page(page);
  426. }
  427. count += loop;
  428. if (loop < n) {
  429. for (; loop < n; loop++)
  430. put_page(pages[loop]);
  431. goto no_more;
  432. }
  433. start += loop;
  434. } while (start <= final_page && count < 65536);
  435. no_more:
  436. /* We now have a contiguous set of dirty pages, each with writeback
  437. * set; the first page is still locked at this point, but all the rest
  438. * have been unlocked.
  439. */
  440. unlock_page(primary_page);
  441. first = primary_page->index;
  442. last = first + count - 1;
  443. _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
  444. ret = afs_store_data(mapping, first, last, offset, to);
  445. switch (ret) {
  446. case 0:
  447. ret = count;
  448. break;
  449. default:
  450. pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
  451. /* Fall through */
  452. case -EACCES:
  453. case -EPERM:
  454. case -ENOKEY:
  455. case -EKEYEXPIRED:
  456. case -EKEYREJECTED:
  457. case -EKEYREVOKED:
  458. afs_redirty_pages(wbc, mapping, first, last);
  459. mapping_set_error(mapping, ret);
  460. break;
  461. case -EDQUOT:
  462. case -ENOSPC:
  463. afs_redirty_pages(wbc, mapping, first, last);
  464. mapping_set_error(mapping, -ENOSPC);
  465. break;
  466. case -EROFS:
  467. case -EIO:
  468. case -EREMOTEIO:
  469. case -EFBIG:
  470. case -ENOENT:
  471. case -ENOMEDIUM:
  472. case -ENXIO:
  473. trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
  474. afs_kill_pages(mapping, first, last);
  475. mapping_set_error(mapping, ret);
  476. break;
  477. }
  478. _leave(" = %d", ret);
  479. return ret;
  480. }
  481. /*
  482. * write a page back to the server
  483. * - the caller locked the page for us
  484. */
  485. int afs_writepage(struct page *page, struct writeback_control *wbc)
  486. {
  487. int ret;
  488. _enter("{%lx},", page->index);
  489. ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
  490. wbc->range_end >> PAGE_SHIFT);
  491. if (ret < 0) {
  492. _leave(" = %d", ret);
  493. return 0;
  494. }
  495. wbc->nr_to_write -= ret;
  496. _leave(" = 0");
  497. return 0;
  498. }
  499. /*
  500. * write a region of pages back to the server
  501. */
  502. static int afs_writepages_region(struct address_space *mapping,
  503. struct writeback_control *wbc,
  504. pgoff_t index, pgoff_t end, pgoff_t *_next)
  505. {
  506. struct page *page;
  507. int ret, n;
  508. _enter(",,%lx,%lx,", index, end);
  509. do {
  510. n = find_get_pages_range_tag(mapping, &index, end,
  511. PAGECACHE_TAG_DIRTY, 1, &page);
  512. if (!n)
  513. break;
  514. _debug("wback %lx", page->index);
  515. /*
  516. * at this point we hold neither the i_pages lock nor the
  517. * page lock: the page may be truncated or invalidated
  518. * (changing page->mapping to NULL), or even swizzled
  519. * back from swapper_space to tmpfs file mapping
  520. */
  521. ret = lock_page_killable(page);
  522. if (ret < 0) {
  523. put_page(page);
  524. _leave(" = %d", ret);
  525. return ret;
  526. }
  527. if (page->mapping != mapping || !PageDirty(page)) {
  528. unlock_page(page);
  529. put_page(page);
  530. continue;
  531. }
  532. if (PageWriteback(page)) {
  533. unlock_page(page);
  534. if (wbc->sync_mode != WB_SYNC_NONE)
  535. wait_on_page_writeback(page);
  536. put_page(page);
  537. continue;
  538. }
  539. if (!clear_page_dirty_for_io(page))
  540. BUG();
  541. ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
  542. put_page(page);
  543. if (ret < 0) {
  544. _leave(" = %d", ret);
  545. return ret;
  546. }
  547. wbc->nr_to_write -= ret;
  548. cond_resched();
  549. } while (index < end && wbc->nr_to_write > 0);
  550. *_next = index;
  551. _leave(" = 0 [%lx]", *_next);
  552. return 0;
  553. }
  554. /*
  555. * write some of the pending data back to the server
  556. */
  557. int afs_writepages(struct address_space *mapping,
  558. struct writeback_control *wbc)
  559. {
  560. pgoff_t start, end, next;
  561. int ret;
  562. _enter("");
  563. if (wbc->range_cyclic) {
  564. start = mapping->writeback_index;
  565. end = -1;
  566. ret = afs_writepages_region(mapping, wbc, start, end, &next);
  567. if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
  568. ret = afs_writepages_region(mapping, wbc, 0, start,
  569. &next);
  570. mapping->writeback_index = next;
  571. } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
  572. end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
  573. ret = afs_writepages_region(mapping, wbc, 0, end, &next);
  574. if (wbc->nr_to_write > 0)
  575. mapping->writeback_index = next;
  576. } else {
  577. start = wbc->range_start >> PAGE_SHIFT;
  578. end = wbc->range_end >> PAGE_SHIFT;
  579. ret = afs_writepages_region(mapping, wbc, start, end, &next);
  580. }
  581. _leave(" = %d", ret);
  582. return ret;
  583. }
  584. /*
  585. * completion of write to server
  586. */
  587. void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
  588. {
  589. struct pagevec pv;
  590. unsigned long priv;
  591. unsigned count, loop;
  592. pgoff_t first = call->first, last = call->last;
  593. _enter("{%llx:%llu},{%lx-%lx}",
  594. vnode->fid.vid, vnode->fid.vnode, first, last);
  595. pagevec_init(&pv);
  596. do {
  597. _debug("done %lx-%lx", first, last);
  598. count = last - first + 1;
  599. if (count > PAGEVEC_SIZE)
  600. count = PAGEVEC_SIZE;
  601. pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
  602. first, count, pv.pages);
  603. ASSERTCMP(pv.nr, ==, count);
  604. for (loop = 0; loop < count; loop++) {
  605. priv = page_private(pv.pages[loop]);
  606. trace_afs_page_dirty(vnode, tracepoint_string("clear"),
  607. pv.pages[loop]->index, priv);
  608. set_page_private(pv.pages[loop], 0);
  609. end_page_writeback(pv.pages[loop]);
  610. }
  611. first += count;
  612. __pagevec_release(&pv);
  613. } while (first <= last);
  614. afs_prune_wb_keys(vnode);
  615. _leave("");
  616. }
  617. /*
  618. * write to an AFS file
  619. */
  620. ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
  621. {
  622. struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
  623. ssize_t result;
  624. size_t count = iov_iter_count(from);
  625. _enter("{%llx:%llu},{%zu},",
  626. vnode->fid.vid, vnode->fid.vnode, count);
  627. if (IS_SWAPFILE(&vnode->vfs_inode)) {
  628. printk(KERN_INFO
  629. "AFS: Attempt to write to active swap file!\n");
  630. return -EBUSY;
  631. }
  632. if (!count)
  633. return 0;
  634. result = generic_file_write_iter(iocb, from);
  635. _leave(" = %zd", result);
  636. return result;
  637. }
  638. /*
  639. * flush any dirty pages for this process, and check for write errors.
  640. * - the return status from this call provides a reliable indication of
  641. * whether any write errors occurred for this process.
  642. */
  643. int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  644. {
  645. struct inode *inode = file_inode(file);
  646. struct afs_vnode *vnode = AFS_FS_I(inode);
  647. _enter("{%llx:%llu},{n=%pD},%d",
  648. vnode->fid.vid, vnode->fid.vnode, file,
  649. datasync);
  650. return file_write_and_wait_range(file, start, end);
  651. }
  652. /*
  653. * notification that a previously read-only page is about to become writable
  654. * - if it returns an error, the caller will deliver a bus error signal
  655. */
  656. vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
  657. {
  658. struct file *file = vmf->vma->vm_file;
  659. struct inode *inode = file_inode(file);
  660. struct afs_vnode *vnode = AFS_FS_I(inode);
  661. unsigned long priv;
  662. _enter("{{%llx:%llu}},{%lx}",
  663. vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
  664. sb_start_pagefault(inode->i_sb);
  665. /* Wait for the page to be written to the cache before we allow it to
  666. * be modified. We then assume the entire page will need writing back.
  667. */
  668. #ifdef CONFIG_AFS_FSCACHE
  669. fscache_wait_on_page_write(vnode->cache, vmf->page);
  670. #endif
  671. if (PageWriteback(vmf->page) &&
  672. wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
  673. return VM_FAULT_RETRY;
  674. if (lock_page_killable(vmf->page) < 0)
  675. return VM_FAULT_RETRY;
  676. /* We mustn't change page->private until writeback is complete as that
  677. * details the portion of the page we need to write back and we might
  678. * need to redirty the page if there's a problem.
  679. */
  680. wait_on_page_writeback(vmf->page);
  681. priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
  682. priv |= 0; /* From */
  683. trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
  684. vmf->page->index, priv);
  685. SetPagePrivate(vmf->page);
  686. set_page_private(vmf->page, priv);
  687. sb_end_pagefault(inode->i_sb);
  688. return VM_FAULT_LOCKED;
  689. }
  690. /*
  691. * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
  692. */
  693. void afs_prune_wb_keys(struct afs_vnode *vnode)
  694. {
  695. LIST_HEAD(graveyard);
  696. struct afs_wb_key *wbk, *tmp;
  697. /* Discard unused keys */
  698. spin_lock(&vnode->wb_lock);
  699. if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
  700. !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
  701. list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
  702. if (refcount_read(&wbk->usage) == 1)
  703. list_move(&wbk->vnode_link, &graveyard);
  704. }
  705. }
  706. spin_unlock(&vnode->wb_lock);
  707. while (!list_empty(&graveyard)) {
  708. wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
  709. list_del(&wbk->vnode_link);
  710. afs_put_wb_key(wbk);
  711. }
  712. }
  713. /*
  714. * Clean up a page during invalidation.
  715. */
  716. int afs_launder_page(struct page *page)
  717. {
  718. struct address_space *mapping = page->mapping;
  719. struct afs_vnode *vnode = AFS_FS_I(mapping->host);
  720. unsigned long priv;
  721. unsigned int f, t;
  722. int ret = 0;
  723. _enter("{%lx}", page->index);
  724. priv = page_private(page);
  725. if (clear_page_dirty_for_io(page)) {
  726. f = 0;
  727. t = PAGE_SIZE;
  728. if (PagePrivate(page)) {
  729. f = priv & AFS_PRIV_MAX;
  730. t = priv >> AFS_PRIV_SHIFT;
  731. }
  732. trace_afs_page_dirty(vnode, tracepoint_string("launder"),
  733. page->index, priv);
  734. ret = afs_store_data(mapping, page->index, page->index, t, f);
  735. }
  736. trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
  737. page->index, priv);
  738. set_page_private(page, 0);
  739. ClearPagePrivate(page);
  740. #ifdef CONFIG_AFS_FSCACHE
  741. if (PageFsCache(page)) {
  742. fscache_wait_on_page_write(vnode->cache, page);
  743. fscache_uncache_page(vnode->cache, page);
  744. }
  745. #endif
  746. return ret;
  747. }