page-io.c 12 KB

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