page-io.c 13 KB

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