page-io.c 14 KB

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