page-io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * linux/fs/ext4/page-io.c
  3. *
  4. * This contains the new page_io functions for ext4
  5. *
  6. * Written by Theodore Ts'o, 2010.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/time.h>
  10. #include <linux/jbd2.h>
  11. #include <linux/highuid.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/string.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/writeback.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/mpage.h>
  19. #include <linux/namei.h>
  20. #include <linux/uio.h>
  21. #include <linux/bio.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include "ext4_jbd2.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. static struct kmem_cache *io_end_cachep;
  30. int __init ext4_init_pageio(void)
  31. {
  32. io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
  33. if (io_end_cachep == NULL)
  34. return -ENOMEM;
  35. return 0;
  36. }
  37. void ext4_exit_pageio(void)
  38. {
  39. kmem_cache_destroy(io_end_cachep);
  40. }
  41. /*
  42. * This function is called by ext4_evict_inode() to make sure there is
  43. * no more pending I/O completion work left to do.
  44. */
  45. void ext4_ioend_shutdown(struct inode *inode)
  46. {
  47. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  48. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
  49. /*
  50. * We need to make sure the work structure is finished being
  51. * used before we let the inode get destroyed.
  52. */
  53. if (work_pending(&EXT4_I(inode)->i_unwritten_work))
  54. cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
  55. }
  56. void ext4_free_io_end(ext4_io_end_t *io)
  57. {
  58. int i;
  59. BUG_ON(!io);
  60. BUG_ON(!list_empty(&io->list));
  61. BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
  62. if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
  63. wake_up_all(ext4_ioend_wq(io->inode));
  64. kmem_cache_free(io_end_cachep, io);
  65. }
  66. /* check a range of space and convert unwritten extents to written. */
  67. static int ext4_end_io(ext4_io_end_t *io)
  68. {
  69. struct inode *inode = io->inode;
  70. loff_t offset = io->offset;
  71. ssize_t size = io->size;
  72. int ret = 0;
  73. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  74. "list->prev 0x%p\n",
  75. io, inode->i_ino, io->list.next, io->list.prev);
  76. ret = ext4_convert_unwritten_extents(inode, offset, size);
  77. if (ret < 0) {
  78. ext4_msg(inode->i_sb, KERN_EMERG,
  79. "failed to convert unwritten extents to written "
  80. "extents -- potential data loss! "
  81. "(inode %lu, offset %llu, size %zd, error %d)",
  82. inode->i_ino, offset, size, ret);
  83. }
  84. /* Wake up anyone waiting on unwritten extent conversion */
  85. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  86. wake_up_all(ext4_ioend_wq(inode));
  87. if (io->flag & EXT4_IO_END_DIRECT)
  88. inode_dio_done(inode);
  89. if (io->iocb)
  90. aio_complete(io->iocb, io->result, 0);
  91. return ret;
  92. }
  93. static void dump_completed_IO(struct inode *inode)
  94. {
  95. #ifdef EXT4FS_DEBUG
  96. struct list_head *cur, *before, *after;
  97. ext4_io_end_t *io, *io0, *io1;
  98. if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
  99. ext4_debug("inode %lu completed_io list is empty\n",
  100. inode->i_ino);
  101. return;
  102. }
  103. ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
  104. list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
  105. cur = &io->list;
  106. before = cur->prev;
  107. io0 = container_of(before, ext4_io_end_t, list);
  108. after = cur->next;
  109. io1 = container_of(after, ext4_io_end_t, list);
  110. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  111. io, inode->i_ino, io0, io1);
  112. }
  113. #endif
  114. }
  115. /* Add the io_end to per-inode completed end_io list. */
  116. void ext4_add_complete_io(ext4_io_end_t *io_end)
  117. {
  118. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  119. struct workqueue_struct *wq;
  120. unsigned long flags;
  121. BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  122. wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
  123. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  124. if (list_empty(&ei->i_completed_io_list))
  125. queue_work(wq, &ei->i_unwritten_work);
  126. list_add_tail(&io_end->list, &ei->i_completed_io_list);
  127. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  128. }
  129. static int ext4_do_flush_completed_IO(struct inode *inode)
  130. {
  131. ext4_io_end_t *io;
  132. struct list_head unwritten;
  133. unsigned long flags;
  134. struct ext4_inode_info *ei = EXT4_I(inode);
  135. int err, ret = 0;
  136. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  137. dump_completed_IO(inode);
  138. list_replace_init(&ei->i_completed_io_list, &unwritten);
  139. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  140. while (!list_empty(&unwritten)) {
  141. io = list_entry(unwritten.next, ext4_io_end_t, list);
  142. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  143. list_del_init(&io->list);
  144. err = ext4_end_io(io);
  145. if (unlikely(!ret && err))
  146. ret = err;
  147. io->flag &= ~EXT4_IO_END_UNWRITTEN;
  148. ext4_free_io_end(io);
  149. }
  150. return ret;
  151. }
  152. /*
  153. * work on completed aio dio IO, to convert unwritten extents to extents
  154. */
  155. void ext4_end_io_work(struct work_struct *work)
  156. {
  157. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  158. i_unwritten_work);
  159. ext4_do_flush_completed_IO(&ei->vfs_inode);
  160. }
  161. int ext4_flush_unwritten_io(struct inode *inode)
  162. {
  163. int ret;
  164. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
  165. !(inode->i_state & I_FREEING));
  166. ret = ext4_do_flush_completed_IO(inode);
  167. ext4_unwritten_wait(inode);
  168. return ret;
  169. }
  170. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  171. {
  172. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  173. if (io) {
  174. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  175. io->inode = inode;
  176. INIT_LIST_HEAD(&io->list);
  177. }
  178. return io;
  179. }
  180. /*
  181. * Print an buffer I/O error compatible with the fs/buffer.c. This
  182. * provides compatibility with dmesg scrapers that look for a specific
  183. * buffer I/O error message. We really need a unified error reporting
  184. * structure to userspace ala Digital Unix's uerf system, but it's
  185. * probably not going to happen in my lifetime, due to LKML politics...
  186. */
  187. static void buffer_io_error(struct buffer_head *bh)
  188. {
  189. char b[BDEVNAME_SIZE];
  190. printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
  191. bdevname(bh->b_bdev, b),
  192. (unsigned long long)bh->b_blocknr);
  193. }
  194. static void ext4_end_bio(struct bio *bio, int error)
  195. {
  196. ext4_io_end_t *io_end = bio->bi_private;
  197. struct inode *inode;
  198. int i;
  199. int blocksize;
  200. sector_t bi_sector = bio->bi_sector;
  201. BUG_ON(!io_end);
  202. inode = io_end->inode;
  203. blocksize = 1 << inode->i_blkbits;
  204. bio->bi_private = NULL;
  205. bio->bi_end_io = NULL;
  206. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  207. error = 0;
  208. for (i = 0; i < bio->bi_vcnt; i++) {
  209. struct bio_vec *bvec = &bio->bi_io_vec[i];
  210. struct page *page = bvec->bv_page;
  211. struct buffer_head *bh, *head;
  212. unsigned bio_start = bvec->bv_offset;
  213. unsigned bio_end = bio_start + bvec->bv_len;
  214. unsigned under_io = 0;
  215. unsigned long flags;
  216. if (!page)
  217. continue;
  218. if (error) {
  219. SetPageError(page);
  220. set_bit(AS_EIO, &page->mapping->flags);
  221. }
  222. bh = head = page_buffers(page);
  223. /*
  224. * We check all buffers in the page under BH_Uptodate_Lock
  225. * to avoid races with other end io clearing async_write flags
  226. */
  227. local_irq_save(flags);
  228. bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
  229. do {
  230. if (bh_offset(bh) < bio_start ||
  231. bh_offset(bh) + blocksize > bio_end) {
  232. if (buffer_async_write(bh))
  233. under_io++;
  234. continue;
  235. }
  236. clear_buffer_async_write(bh);
  237. if (error)
  238. buffer_io_error(bh);
  239. } while ((bh = bh->b_this_page) != head);
  240. bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
  241. local_irq_restore(flags);
  242. if (!under_io)
  243. end_page_writeback(page);
  244. }
  245. bio_put(bio);
  246. if (error) {
  247. io_end->flag |= EXT4_IO_END_ERROR;
  248. ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
  249. "(offset %llu size %ld starting block %llu)",
  250. inode->i_ino,
  251. (unsigned long long) io_end->offset,
  252. (long) io_end->size,
  253. (unsigned long long)
  254. bi_sector >> (inode->i_blkbits - 9));
  255. }
  256. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  257. ext4_free_io_end(io_end);
  258. return;
  259. }
  260. ext4_add_complete_io(io_end);
  261. }
  262. void ext4_io_submit(struct ext4_io_submit *io)
  263. {
  264. struct bio *bio = io->io_bio;
  265. if (bio) {
  266. bio_get(io->io_bio);
  267. submit_bio(io->io_op, io->io_bio);
  268. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  269. bio_put(io->io_bio);
  270. }
  271. io->io_bio = NULL;
  272. io->io_op = 0;
  273. io->io_end = NULL;
  274. }
  275. static int io_submit_init(struct ext4_io_submit *io,
  276. struct inode *inode,
  277. struct writeback_control *wbc,
  278. struct buffer_head *bh)
  279. {
  280. ext4_io_end_t *io_end;
  281. struct page *page = bh->b_page;
  282. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  283. struct bio *bio;
  284. io_end = ext4_init_io_end(inode, GFP_NOFS);
  285. if (!io_end)
  286. return -ENOMEM;
  287. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  288. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  289. bio->bi_bdev = bh->b_bdev;
  290. bio->bi_private = io->io_end = io_end;
  291. bio->bi_end_io = ext4_end_bio;
  292. io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
  293. io->io_bio = bio;
  294. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  295. io->io_next_block = bh->b_blocknr;
  296. return 0;
  297. }
  298. static int io_submit_add_bh(struct ext4_io_submit *io,
  299. struct inode *inode,
  300. struct writeback_control *wbc,
  301. struct buffer_head *bh)
  302. {
  303. ext4_io_end_t *io_end;
  304. int ret;
  305. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  306. submit_and_retry:
  307. ext4_io_submit(io);
  308. }
  309. if (io->io_bio == NULL) {
  310. ret = io_submit_init(io, inode, wbc, bh);
  311. if (ret)
  312. return ret;
  313. }
  314. io_end = io->io_end;
  315. if (buffer_uninit(bh))
  316. ext4_set_io_unwritten_flag(inode, io_end);
  317. io->io_end->size += bh->b_size;
  318. io->io_next_block++;
  319. ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
  320. if (ret != bh->b_size)
  321. goto submit_and_retry;
  322. return 0;
  323. }
  324. int ext4_bio_write_page(struct ext4_io_submit *io,
  325. struct page *page,
  326. int len,
  327. struct writeback_control *wbc)
  328. {
  329. struct inode *inode = page->mapping->host;
  330. unsigned block_start, blocksize;
  331. struct buffer_head *bh, *head;
  332. int ret = 0;
  333. int nr_submitted = 0;
  334. blocksize = 1 << inode->i_blkbits;
  335. BUG_ON(!PageLocked(page));
  336. BUG_ON(PageWriteback(page));
  337. set_page_writeback(page);
  338. ClearPageError(page);
  339. /*
  340. * In the first loop we prepare and mark buffers to submit. We have to
  341. * mark all buffers in the page before submitting so that
  342. * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
  343. * on the first buffer finishes and we are still working on submitting
  344. * the second buffer.
  345. */
  346. bh = head = page_buffers(page);
  347. do {
  348. block_start = bh_offset(bh);
  349. if (block_start >= len) {
  350. /*
  351. * Comments copied from block_write_full_page_endio:
  352. *
  353. * The page straddles i_size. It must be zeroed out on
  354. * each and every writepage invocation because it may
  355. * be mmapped. "A file is mapped in multiples of the
  356. * page size. For a file that is not a multiple of
  357. * the page size, the remaining memory is zeroed when
  358. * mapped, and writes to that region are not written
  359. * out to the file."
  360. */
  361. zero_user_segment(page, block_start,
  362. block_start + blocksize);
  363. clear_buffer_dirty(bh);
  364. set_buffer_uptodate(bh);
  365. continue;
  366. }
  367. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  368. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  369. /* A hole? We can safely clear the dirty bit */
  370. if (!buffer_mapped(bh))
  371. clear_buffer_dirty(bh);
  372. if (io->io_bio)
  373. ext4_io_submit(io);
  374. continue;
  375. }
  376. if (buffer_new(bh)) {
  377. clear_buffer_new(bh);
  378. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  379. }
  380. set_buffer_async_write(bh);
  381. } while ((bh = bh->b_this_page) != head);
  382. /* Now submit buffers to write */
  383. bh = head = page_buffers(page);
  384. do {
  385. if (!buffer_async_write(bh))
  386. continue;
  387. ret = io_submit_add_bh(io, inode, wbc, bh);
  388. if (ret) {
  389. /*
  390. * We only get here on ENOMEM. Not much else
  391. * we can do but mark the page as dirty, and
  392. * better luck next time.
  393. */
  394. redirty_page_for_writepage(wbc, page);
  395. break;
  396. }
  397. nr_submitted++;
  398. clear_buffer_dirty(bh);
  399. } while ((bh = bh->b_this_page) != head);
  400. /* Error stopped previous loop? Clean up buffers... */
  401. if (ret) {
  402. do {
  403. clear_buffer_async_write(bh);
  404. bh = bh->b_this_page;
  405. } while (bh != head);
  406. }
  407. unlock_page(page);
  408. /* Nothing submitted - we have to end page writeback */
  409. if (!nr_submitted)
  410. end_page_writeback(page);
  411. return ret;
  412. }