page-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. for (i = 0; i < bio->bi_vcnt; i++) {
  62. struct bio_vec *bvec = &bio->bi_io_vec[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_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 writing to inode %lu "
  273. "(offset %llu size %ld starting block %llu)",
  274. 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. }
  280. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  281. /*
  282. * Link bio into list hanging from io_end. We have to do it
  283. * atomically as bio completions can be racing against each
  284. * other.
  285. */
  286. bio->bi_private = xchg(&io_end->bio, bio);
  287. ext4_put_io_end_defer(io_end);
  288. } else {
  289. /*
  290. * Drop io_end reference early. Inode can get freed once
  291. * we finish the bio.
  292. */
  293. ext4_put_io_end_defer(io_end);
  294. ext4_finish_bio(bio);
  295. bio_put(bio);
  296. }
  297. }
  298. void ext4_io_submit(struct ext4_io_submit *io)
  299. {
  300. struct bio *bio = io->io_bio;
  301. if (bio) {
  302. bio_get(io->io_bio);
  303. submit_bio(io->io_op, io->io_bio);
  304. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  305. bio_put(io->io_bio);
  306. }
  307. io->io_bio = NULL;
  308. }
  309. void ext4_io_submit_init(struct ext4_io_submit *io,
  310. struct writeback_control *wbc)
  311. {
  312. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  313. io->io_bio = NULL;
  314. io->io_end = NULL;
  315. }
  316. static int io_submit_init_bio(struct ext4_io_submit *io,
  317. struct buffer_head *bh)
  318. {
  319. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  320. struct bio *bio;
  321. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  322. if (!bio)
  323. return -ENOMEM;
  324. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  325. bio->bi_bdev = bh->b_bdev;
  326. bio->bi_end_io = ext4_end_bio;
  327. bio->bi_private = ext4_get_io_end(io->io_end);
  328. io->io_bio = bio;
  329. io->io_next_block = bh->b_blocknr;
  330. return 0;
  331. }
  332. static int io_submit_add_bh(struct ext4_io_submit *io,
  333. struct inode *inode,
  334. struct buffer_head *bh)
  335. {
  336. int ret;
  337. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  338. submit_and_retry:
  339. ext4_io_submit(io);
  340. }
  341. if (io->io_bio == NULL) {
  342. ret = io_submit_init_bio(io, bh);
  343. if (ret)
  344. return ret;
  345. }
  346. ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
  347. if (ret != bh->b_size)
  348. goto submit_and_retry;
  349. io->io_next_block++;
  350. return 0;
  351. }
  352. int ext4_bio_write_page(struct ext4_io_submit *io,
  353. struct page *page,
  354. int len,
  355. struct writeback_control *wbc)
  356. {
  357. struct inode *inode = page->mapping->host;
  358. unsigned block_start, blocksize;
  359. struct buffer_head *bh, *head;
  360. int ret = 0;
  361. int nr_submitted = 0;
  362. blocksize = 1 << inode->i_blkbits;
  363. BUG_ON(!PageLocked(page));
  364. BUG_ON(PageWriteback(page));
  365. set_page_writeback(page);
  366. ClearPageError(page);
  367. /*
  368. * In the first loop we prepare and mark buffers to submit. We have to
  369. * mark all buffers in the page before submitting so that
  370. * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
  371. * on the first buffer finishes and we are still working on submitting
  372. * the second buffer.
  373. */
  374. bh = head = page_buffers(page);
  375. do {
  376. block_start = bh_offset(bh);
  377. if (block_start >= len) {
  378. /*
  379. * Comments copied from block_write_full_page_endio:
  380. *
  381. * The page straddles i_size. It must be zeroed out on
  382. * each and every writepage invocation because it may
  383. * be mmapped. "A file is mapped in multiples of the
  384. * page size. For a file that is not a multiple of
  385. * the page size, the remaining memory is zeroed when
  386. * mapped, and writes to that region are not written
  387. * out to the file."
  388. */
  389. zero_user_segment(page, block_start,
  390. block_start + blocksize);
  391. clear_buffer_dirty(bh);
  392. set_buffer_uptodate(bh);
  393. continue;
  394. }
  395. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  396. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  397. /* A hole? We can safely clear the dirty bit */
  398. if (!buffer_mapped(bh))
  399. clear_buffer_dirty(bh);
  400. if (io->io_bio)
  401. ext4_io_submit(io);
  402. continue;
  403. }
  404. if (buffer_new(bh)) {
  405. clear_buffer_new(bh);
  406. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  407. }
  408. set_buffer_async_write(bh);
  409. } while ((bh = bh->b_this_page) != head);
  410. /* Now submit buffers to write */
  411. bh = head = page_buffers(page);
  412. do {
  413. if (!buffer_async_write(bh))
  414. continue;
  415. ret = io_submit_add_bh(io, inode, bh);
  416. if (ret) {
  417. /*
  418. * We only get here on ENOMEM. Not much else
  419. * we can do but mark the page as dirty, and
  420. * better luck next time.
  421. */
  422. redirty_page_for_writepage(wbc, page);
  423. break;
  424. }
  425. nr_submitted++;
  426. clear_buffer_dirty(bh);
  427. } while ((bh = bh->b_this_page) != head);
  428. /* Error stopped previous loop? Clean up buffers... */
  429. if (ret) {
  430. do {
  431. clear_buffer_async_write(bh);
  432. bh = bh->b_this_page;
  433. } while (bh != head);
  434. }
  435. unlock_page(page);
  436. /* Nothing submitted - we have to end page writeback */
  437. if (!nr_submitted)
  438. end_page_writeback(page);
  439. return ret;
  440. }