page-io.c 13 KB

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