page-io.c 13 KB

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