write.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. static int afs_write_back_from_locked_page(struct afs_writeback *wb,
  19. struct page *page);
  20. /*
  21. * mark a page as having been made dirty and thus needing writeback
  22. */
  23. int afs_set_page_dirty(struct page *page)
  24. {
  25. _enter("");
  26. return __set_page_dirty_nobuffers(page);
  27. }
  28. /*
  29. * unlink a writeback record because its usage has reached zero
  30. * - must be called with the wb->vnode->writeback_lock held
  31. */
  32. static void afs_unlink_writeback(struct afs_writeback *wb)
  33. {
  34. struct afs_writeback *front;
  35. struct afs_vnode *vnode = wb->vnode;
  36. list_del_init(&wb->link);
  37. if (!list_empty(&vnode->writebacks)) {
  38. /* if an fsync rises to the front of the queue then wake it
  39. * up */
  40. front = list_entry(vnode->writebacks.next,
  41. struct afs_writeback, link);
  42. if (front->state == AFS_WBACK_SYNCING) {
  43. _debug("wake up sync");
  44. front->state = AFS_WBACK_COMPLETE;
  45. wake_up(&front->waitq);
  46. }
  47. }
  48. }
  49. /*
  50. * free a writeback record
  51. */
  52. static void afs_free_writeback(struct afs_writeback *wb)
  53. {
  54. _enter("");
  55. key_put(wb->key);
  56. kfree(wb);
  57. }
  58. /*
  59. * dispose of a reference to a writeback record
  60. */
  61. void afs_put_writeback(struct afs_writeback *wb)
  62. {
  63. struct afs_vnode *vnode = wb->vnode;
  64. _enter("{%d}", wb->usage);
  65. spin_lock(&vnode->writeback_lock);
  66. if (--wb->usage == 0)
  67. afs_unlink_writeback(wb);
  68. else
  69. wb = NULL;
  70. spin_unlock(&vnode->writeback_lock);
  71. if (wb)
  72. afs_free_writeback(wb);
  73. }
  74. /*
  75. * partly or wholly fill a page that's under preparation for writing
  76. */
  77. static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
  78. loff_t pos, struct page *page)
  79. {
  80. loff_t i_size;
  81. int ret;
  82. int len;
  83. _enter(",,%llu", (unsigned long long)pos);
  84. i_size = i_size_read(&vnode->vfs_inode);
  85. if (pos + PAGE_CACHE_SIZE > i_size)
  86. len = i_size - pos;
  87. else
  88. len = PAGE_CACHE_SIZE;
  89. ret = afs_vnode_fetch_data(vnode, key, pos, len, page);
  90. if (ret < 0) {
  91. if (ret == -ENOENT) {
  92. _debug("got NOENT from server"
  93. " - marking file deleted and stale");
  94. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  95. ret = -ESTALE;
  96. }
  97. }
  98. _leave(" = %d", ret);
  99. return ret;
  100. }
  101. /*
  102. * prepare to perform part of a write to a page
  103. */
  104. int afs_write_begin(struct file *file, struct address_space *mapping,
  105. loff_t pos, unsigned len, unsigned flags,
  106. struct page **pagep, void **fsdata)
  107. {
  108. struct afs_writeback *candidate, *wb;
  109. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  110. struct page *page;
  111. struct key *key = file->private_data;
  112. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  113. unsigned to = from + len;
  114. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  115. int ret;
  116. _enter("{%x:%u},{%lx},%u,%u",
  117. vnode->fid.vid, vnode->fid.vnode, index, from, to);
  118. candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
  119. if (!candidate)
  120. return -ENOMEM;
  121. candidate->vnode = vnode;
  122. candidate->first = candidate->last = index;
  123. candidate->offset_first = from;
  124. candidate->to_last = to;
  125. INIT_LIST_HEAD(&candidate->link);
  126. candidate->usage = 1;
  127. candidate->state = AFS_WBACK_PENDING;
  128. init_waitqueue_head(&candidate->waitq);
  129. page = grab_cache_page_write_begin(mapping, index, flags);
  130. if (!page) {
  131. kfree(candidate);
  132. return -ENOMEM;
  133. }
  134. if (!PageUptodate(page) && len != PAGE_CACHE_SIZE) {
  135. ret = afs_fill_page(vnode, key, index << PAGE_CACHE_SHIFT, page);
  136. if (ret < 0) {
  137. unlock_page(page);
  138. put_page(page);
  139. kfree(candidate);
  140. _leave(" = %d [prep]", ret);
  141. return ret;
  142. }
  143. SetPageUptodate(page);
  144. }
  145. /* page won't leak in error case: it eventually gets cleaned off LRU */
  146. *pagep = page;
  147. try_again:
  148. spin_lock(&vnode->writeback_lock);
  149. /* see if this page is already pending a writeback under a suitable key
  150. * - if so we can just join onto that one */
  151. wb = (struct afs_writeback *) page_private(page);
  152. if (wb) {
  153. if (wb->key == key && wb->state == AFS_WBACK_PENDING)
  154. goto subsume_in_current_wb;
  155. goto flush_conflicting_wb;
  156. }
  157. if (index > 0) {
  158. /* see if we can find an already pending writeback that we can
  159. * append this page to */
  160. list_for_each_entry(wb, &vnode->writebacks, link) {
  161. if (wb->last == index - 1 && wb->key == key &&
  162. wb->state == AFS_WBACK_PENDING)
  163. goto append_to_previous_wb;
  164. }
  165. }
  166. list_add_tail(&candidate->link, &vnode->writebacks);
  167. candidate->key = key_get(key);
  168. spin_unlock(&vnode->writeback_lock);
  169. SetPagePrivate(page);
  170. set_page_private(page, (unsigned long) candidate);
  171. _leave(" = 0 [new]");
  172. return 0;
  173. subsume_in_current_wb:
  174. _debug("subsume");
  175. ASSERTRANGE(wb->first, <=, index, <=, wb->last);
  176. if (index == wb->first && from < wb->offset_first)
  177. wb->offset_first = from;
  178. if (index == wb->last && to > wb->to_last)
  179. wb->to_last = to;
  180. spin_unlock(&vnode->writeback_lock);
  181. kfree(candidate);
  182. _leave(" = 0 [sub]");
  183. return 0;
  184. append_to_previous_wb:
  185. _debug("append into %lx-%lx", wb->first, wb->last);
  186. wb->usage++;
  187. wb->last++;
  188. wb->to_last = to;
  189. spin_unlock(&vnode->writeback_lock);
  190. SetPagePrivate(page);
  191. set_page_private(page, (unsigned long) wb);
  192. kfree(candidate);
  193. _leave(" = 0 [app]");
  194. return 0;
  195. /* the page is currently bound to another context, so if it's dirty we
  196. * need to flush it before we can use the new context */
  197. flush_conflicting_wb:
  198. _debug("flush conflict");
  199. if (wb->state == AFS_WBACK_PENDING)
  200. wb->state = AFS_WBACK_CONFLICTING;
  201. spin_unlock(&vnode->writeback_lock);
  202. if (PageDirty(page)) {
  203. ret = afs_write_back_from_locked_page(wb, page);
  204. if (ret < 0) {
  205. afs_put_writeback(candidate);
  206. _leave(" = %d", ret);
  207. return ret;
  208. }
  209. }
  210. /* the page holds a ref on the writeback record */
  211. afs_put_writeback(wb);
  212. set_page_private(page, 0);
  213. ClearPagePrivate(page);
  214. goto try_again;
  215. }
  216. /*
  217. * finalise part of a write to a page
  218. */
  219. int afs_write_end(struct file *file, struct address_space *mapping,
  220. loff_t pos, unsigned len, unsigned copied,
  221. struct page *page, void *fsdata)
  222. {
  223. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  224. loff_t i_size, maybe_i_size;
  225. _enter("{%x:%u},{%lx}",
  226. vnode->fid.vid, vnode->fid.vnode, page->index);
  227. maybe_i_size = pos + copied;
  228. i_size = i_size_read(&vnode->vfs_inode);
  229. if (maybe_i_size > i_size) {
  230. spin_lock(&vnode->writeback_lock);
  231. i_size = i_size_read(&vnode->vfs_inode);
  232. if (maybe_i_size > i_size)
  233. i_size_write(&vnode->vfs_inode, maybe_i_size);
  234. spin_unlock(&vnode->writeback_lock);
  235. }
  236. set_page_dirty(page);
  237. if (PageDirty(page))
  238. _debug("dirtied");
  239. unlock_page(page);
  240. page_cache_release(page);
  241. return copied;
  242. }
  243. /*
  244. * kill all the pages in the given range
  245. */
  246. static void afs_kill_pages(struct afs_vnode *vnode, bool error,
  247. pgoff_t first, pgoff_t last)
  248. {
  249. struct pagevec pv;
  250. unsigned count, loop;
  251. _enter("{%x:%u},%lx-%lx",
  252. vnode->fid.vid, vnode->fid.vnode, first, last);
  253. pagevec_init(&pv, 0);
  254. do {
  255. _debug("kill %lx-%lx", first, last);
  256. count = last - first + 1;
  257. if (count > PAGEVEC_SIZE)
  258. count = PAGEVEC_SIZE;
  259. pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
  260. first, count, pv.pages);
  261. ASSERTCMP(pv.nr, ==, count);
  262. for (loop = 0; loop < count; loop++) {
  263. struct page *page = pv.pages[loop];
  264. ClearPageUptodate(page);
  265. if (error)
  266. SetPageError(page);
  267. if (PageWriteback(page))
  268. end_page_writeback(page);
  269. if (page->index >= first)
  270. first = page->index + 1;
  271. }
  272. __pagevec_release(&pv);
  273. } while (first < last);
  274. _leave("");
  275. }
  276. /*
  277. * synchronously write back the locked page and any subsequent non-locked dirty
  278. * pages also covered by the same writeback record
  279. */
  280. static int afs_write_back_from_locked_page(struct afs_writeback *wb,
  281. struct page *primary_page)
  282. {
  283. struct page *pages[8], *page;
  284. unsigned long count;
  285. unsigned n, offset, to;
  286. pgoff_t start, first, last;
  287. int loop, ret;
  288. _enter(",%lx", primary_page->index);
  289. count = 1;
  290. if (!clear_page_dirty_for_io(primary_page))
  291. BUG();
  292. if (test_set_page_writeback(primary_page))
  293. BUG();
  294. /* find all consecutive lockable dirty pages, stopping when we find a
  295. * page that is not immediately lockable, is not dirty or is missing,
  296. * or we reach the end of the range */
  297. start = primary_page->index;
  298. if (start >= wb->last)
  299. goto no_more;
  300. start++;
  301. do {
  302. _debug("more %lx [%lx]", start, count);
  303. n = wb->last - start + 1;
  304. if (n > ARRAY_SIZE(pages))
  305. n = ARRAY_SIZE(pages);
  306. n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
  307. start, n, pages);
  308. _debug("fgpc %u", n);
  309. if (n == 0)
  310. goto no_more;
  311. if (pages[0]->index != start) {
  312. do {
  313. put_page(pages[--n]);
  314. } while (n > 0);
  315. goto no_more;
  316. }
  317. for (loop = 0; loop < n; loop++) {
  318. page = pages[loop];
  319. if (page->index > wb->last)
  320. break;
  321. if (!trylock_page(page))
  322. break;
  323. if (!PageDirty(page) ||
  324. page_private(page) != (unsigned long) wb) {
  325. unlock_page(page);
  326. break;
  327. }
  328. if (!clear_page_dirty_for_io(page))
  329. BUG();
  330. if (test_set_page_writeback(page))
  331. BUG();
  332. unlock_page(page);
  333. put_page(page);
  334. }
  335. count += loop;
  336. if (loop < n) {
  337. for (; loop < n; loop++)
  338. put_page(pages[loop]);
  339. goto no_more;
  340. }
  341. start += loop;
  342. } while (start <= wb->last && count < 65536);
  343. no_more:
  344. /* we now have a contiguous set of dirty pages, each with writeback set
  345. * and the dirty mark cleared; the first page is locked and must remain
  346. * so, all the rest are unlocked */
  347. first = primary_page->index;
  348. last = first + count - 1;
  349. offset = (first == wb->first) ? wb->offset_first : 0;
  350. to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
  351. _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
  352. ret = afs_vnode_store_data(wb, first, last, offset, to);
  353. if (ret < 0) {
  354. switch (ret) {
  355. case -EDQUOT:
  356. case -ENOSPC:
  357. set_bit(AS_ENOSPC,
  358. &wb->vnode->vfs_inode.i_mapping->flags);
  359. break;
  360. case -EROFS:
  361. case -EIO:
  362. case -EREMOTEIO:
  363. case -EFBIG:
  364. case -ENOENT:
  365. case -ENOMEDIUM:
  366. case -ENXIO:
  367. afs_kill_pages(wb->vnode, true, first, last);
  368. set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
  369. break;
  370. case -EACCES:
  371. case -EPERM:
  372. case -ENOKEY:
  373. case -EKEYEXPIRED:
  374. case -EKEYREJECTED:
  375. case -EKEYREVOKED:
  376. afs_kill_pages(wb->vnode, false, first, last);
  377. break;
  378. default:
  379. break;
  380. }
  381. } else {
  382. ret = count;
  383. }
  384. _leave(" = %d", ret);
  385. return ret;
  386. }
  387. /*
  388. * write a page back to the server
  389. * - the caller locked the page for us
  390. */
  391. int afs_writepage(struct page *page, struct writeback_control *wbc)
  392. {
  393. struct afs_writeback *wb;
  394. int ret;
  395. _enter("{%lx},", page->index);
  396. wb = (struct afs_writeback *) page_private(page);
  397. ASSERT(wb != NULL);
  398. ret = afs_write_back_from_locked_page(wb, page);
  399. unlock_page(page);
  400. if (ret < 0) {
  401. _leave(" = %d", ret);
  402. return 0;
  403. }
  404. wbc->nr_to_write -= ret;
  405. _leave(" = 0");
  406. return 0;
  407. }
  408. /*
  409. * write a region of pages back to the server
  410. */
  411. static int afs_writepages_region(struct address_space *mapping,
  412. struct writeback_control *wbc,
  413. pgoff_t index, pgoff_t end, pgoff_t *_next)
  414. {
  415. struct afs_writeback *wb;
  416. struct page *page;
  417. int ret, n;
  418. _enter(",,%lx,%lx,", index, end);
  419. do {
  420. n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
  421. 1, &page);
  422. if (!n)
  423. break;
  424. _debug("wback %lx", page->index);
  425. if (page->index > end) {
  426. *_next = index;
  427. page_cache_release(page);
  428. _leave(" = 0 [%lx]", *_next);
  429. return 0;
  430. }
  431. /* at this point we hold neither mapping->tree_lock nor lock on
  432. * the page itself: the page may be truncated or invalidated
  433. * (changing page->mapping to NULL), or even swizzled back from
  434. * swapper_space to tmpfs file mapping
  435. */
  436. lock_page(page);
  437. if (page->mapping != mapping) {
  438. unlock_page(page);
  439. page_cache_release(page);
  440. continue;
  441. }
  442. if (wbc->sync_mode != WB_SYNC_NONE)
  443. wait_on_page_writeback(page);
  444. if (PageWriteback(page) || !PageDirty(page)) {
  445. unlock_page(page);
  446. put_page(page);
  447. continue;
  448. }
  449. wb = (struct afs_writeback *) page_private(page);
  450. ASSERT(wb != NULL);
  451. spin_lock(&wb->vnode->writeback_lock);
  452. wb->state = AFS_WBACK_WRITING;
  453. spin_unlock(&wb->vnode->writeback_lock);
  454. ret = afs_write_back_from_locked_page(wb, page);
  455. unlock_page(page);
  456. page_cache_release(page);
  457. if (ret < 0) {
  458. _leave(" = %d", ret);
  459. return ret;
  460. }
  461. wbc->nr_to_write -= ret;
  462. cond_resched();
  463. } while (index < end && wbc->nr_to_write > 0);
  464. *_next = index;
  465. _leave(" = 0 [%lx]", *_next);
  466. return 0;
  467. }
  468. /*
  469. * write some of the pending data back to the server
  470. */
  471. int afs_writepages(struct address_space *mapping,
  472. struct writeback_control *wbc)
  473. {
  474. pgoff_t start, end, next;
  475. int ret;
  476. _enter("");
  477. if (wbc->range_cyclic) {
  478. start = mapping->writeback_index;
  479. end = -1;
  480. ret = afs_writepages_region(mapping, wbc, start, end, &next);
  481. if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
  482. ret = afs_writepages_region(mapping, wbc, 0, start,
  483. &next);
  484. mapping->writeback_index = next;
  485. } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
  486. end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
  487. ret = afs_writepages_region(mapping, wbc, 0, end, &next);
  488. if (wbc->nr_to_write > 0)
  489. mapping->writeback_index = next;
  490. } else {
  491. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  492. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  493. ret = afs_writepages_region(mapping, wbc, start, end, &next);
  494. }
  495. _leave(" = %d", ret);
  496. return ret;
  497. }
  498. /*
  499. * completion of write to server
  500. */
  501. void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
  502. {
  503. struct afs_writeback *wb = call->wb;
  504. struct pagevec pv;
  505. unsigned count, loop;
  506. pgoff_t first = call->first, last = call->last;
  507. bool free_wb;
  508. _enter("{%x:%u},{%lx-%lx}",
  509. vnode->fid.vid, vnode->fid.vnode, first, last);
  510. ASSERT(wb != NULL);
  511. pagevec_init(&pv, 0);
  512. do {
  513. _debug("done %lx-%lx", first, last);
  514. count = last - first + 1;
  515. if (count > PAGEVEC_SIZE)
  516. count = PAGEVEC_SIZE;
  517. pv.nr = find_get_pages_contig(call->mapping, first, count,
  518. pv.pages);
  519. ASSERTCMP(pv.nr, ==, count);
  520. spin_lock(&vnode->writeback_lock);
  521. for (loop = 0; loop < count; loop++) {
  522. struct page *page = pv.pages[loop];
  523. end_page_writeback(page);
  524. if (page_private(page) == (unsigned long) wb) {
  525. set_page_private(page, 0);
  526. ClearPagePrivate(page);
  527. wb->usage--;
  528. }
  529. }
  530. free_wb = false;
  531. if (wb->usage == 0) {
  532. afs_unlink_writeback(wb);
  533. free_wb = true;
  534. }
  535. spin_unlock(&vnode->writeback_lock);
  536. first += count;
  537. if (free_wb) {
  538. afs_free_writeback(wb);
  539. wb = NULL;
  540. }
  541. __pagevec_release(&pv);
  542. } while (first <= last);
  543. _leave("");
  544. }
  545. /*
  546. * write to an AFS file
  547. */
  548. ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
  549. {
  550. struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
  551. ssize_t result;
  552. size_t count = iov_iter_count(from);
  553. _enter("{%x.%u},{%zu},",
  554. vnode->fid.vid, vnode->fid.vnode, count);
  555. if (IS_SWAPFILE(&vnode->vfs_inode)) {
  556. printk(KERN_INFO
  557. "AFS: Attempt to write to active swap file!\n");
  558. return -EBUSY;
  559. }
  560. if (!count)
  561. return 0;
  562. result = generic_file_write_iter(iocb, from);
  563. if (IS_ERR_VALUE(result)) {
  564. _leave(" = %zd", result);
  565. return result;
  566. }
  567. _leave(" = %zd", result);
  568. return result;
  569. }
  570. /*
  571. * flush the vnode to the fileserver
  572. */
  573. int afs_writeback_all(struct afs_vnode *vnode)
  574. {
  575. struct address_space *mapping = vnode->vfs_inode.i_mapping;
  576. struct writeback_control wbc = {
  577. .sync_mode = WB_SYNC_ALL,
  578. .nr_to_write = LONG_MAX,
  579. .range_cyclic = 1,
  580. };
  581. int ret;
  582. _enter("");
  583. ret = mapping->a_ops->writepages(mapping, &wbc);
  584. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  585. _leave(" = %d", ret);
  586. return ret;
  587. }
  588. /*
  589. * flush any dirty pages for this process, and check for write errors.
  590. * - the return status from this call provides a reliable indication of
  591. * whether any write errors occurred for this process.
  592. */
  593. int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  594. {
  595. struct inode *inode = file_inode(file);
  596. struct afs_writeback *wb, *xwb;
  597. struct afs_vnode *vnode = AFS_FS_I(inode);
  598. int ret;
  599. _enter("{%x:%u},{n=%pD},%d",
  600. vnode->fid.vid, vnode->fid.vnode, file,
  601. datasync);
  602. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  603. if (ret)
  604. return ret;
  605. mutex_lock(&inode->i_mutex);
  606. /* use a writeback record as a marker in the queue - when this reaches
  607. * the front of the queue, all the outstanding writes are either
  608. * completed or rejected */
  609. wb = kzalloc(sizeof(*wb), GFP_KERNEL);
  610. if (!wb) {
  611. ret = -ENOMEM;
  612. goto out;
  613. }
  614. wb->vnode = vnode;
  615. wb->first = 0;
  616. wb->last = -1;
  617. wb->offset_first = 0;
  618. wb->to_last = PAGE_SIZE;
  619. wb->usage = 1;
  620. wb->state = AFS_WBACK_SYNCING;
  621. init_waitqueue_head(&wb->waitq);
  622. spin_lock(&vnode->writeback_lock);
  623. list_for_each_entry(xwb, &vnode->writebacks, link) {
  624. if (xwb->state == AFS_WBACK_PENDING)
  625. xwb->state = AFS_WBACK_CONFLICTING;
  626. }
  627. list_add_tail(&wb->link, &vnode->writebacks);
  628. spin_unlock(&vnode->writeback_lock);
  629. /* push all the outstanding writebacks to the server */
  630. ret = afs_writeback_all(vnode);
  631. if (ret < 0) {
  632. afs_put_writeback(wb);
  633. _leave(" = %d [wb]", ret);
  634. goto out;
  635. }
  636. /* wait for the preceding writes to actually complete */
  637. ret = wait_event_interruptible(wb->waitq,
  638. wb->state == AFS_WBACK_COMPLETE ||
  639. vnode->writebacks.next == &wb->link);
  640. afs_put_writeback(wb);
  641. _leave(" = %d", ret);
  642. out:
  643. mutex_unlock(&inode->i_mutex);
  644. return ret;
  645. }
  646. /*
  647. * Flush out all outstanding writes on a file opened for writing when it is
  648. * closed.
  649. */
  650. int afs_flush(struct file *file, fl_owner_t id)
  651. {
  652. _enter("");
  653. if ((file->f_mode & FMODE_WRITE) == 0)
  654. return 0;
  655. return vfs_fsync(file, 0);
  656. }
  657. /*
  658. * notification that a previously read-only page is about to become writable
  659. * - if it returns an error, the caller will deliver a bus error signal
  660. */
  661. int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  662. {
  663. struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
  664. _enter("{{%x:%u}},{%lx}",
  665. vnode->fid.vid, vnode->fid.vnode, page->index);
  666. /* wait for the page to be written to the cache before we allow it to
  667. * be modified */
  668. #ifdef CONFIG_AFS_FSCACHE
  669. fscache_wait_on_page_write(vnode->cache, page);
  670. #endif
  671. _leave(" = 0");
  672. return 0;
  673. }