page-io.c 13 KB

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