page-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 workqueue_struct *wq;
  177. unsigned long flags;
  178. /* Only reserved conversions from writeback should enter here */
  179. WARN_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  180. WARN_ON(!io_end->handle);
  181. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  182. wq = EXT4_SB(io_end->inode->i_sb)->rsv_conversion_wq;
  183. if (list_empty(&ei->i_rsv_conversion_list))
  184. queue_work(wq, &ei->i_rsv_conversion_work);
  185. list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
  186. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  187. }
  188. static int ext4_do_flush_completed_IO(struct inode *inode,
  189. struct list_head *head)
  190. {
  191. ext4_io_end_t *io;
  192. struct list_head unwritten;
  193. unsigned long flags;
  194. struct ext4_inode_info *ei = EXT4_I(inode);
  195. int err, ret = 0;
  196. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  197. dump_completed_IO(inode, head);
  198. list_replace_init(head, &unwritten);
  199. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  200. while (!list_empty(&unwritten)) {
  201. io = list_entry(unwritten.next, ext4_io_end_t, list);
  202. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  203. list_del_init(&io->list);
  204. err = ext4_end_io(io);
  205. if (unlikely(!ret && err))
  206. ret = err;
  207. }
  208. return ret;
  209. }
  210. /*
  211. * work on completed IO, to convert unwritten extents to extents
  212. */
  213. void ext4_end_io_rsv_work(struct work_struct *work)
  214. {
  215. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  216. i_rsv_conversion_work);
  217. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
  218. }
  219. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  220. {
  221. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  222. if (io) {
  223. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  224. io->inode = inode;
  225. INIT_LIST_HEAD(&io->list);
  226. atomic_set(&io->count, 1);
  227. }
  228. return io;
  229. }
  230. void ext4_put_io_end_defer(ext4_io_end_t *io_end)
  231. {
  232. if (atomic_dec_and_test(&io_end->count)) {
  233. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
  234. ext4_release_io_end(io_end);
  235. return;
  236. }
  237. ext4_add_complete_io(io_end);
  238. }
  239. }
  240. int ext4_put_io_end(ext4_io_end_t *io_end)
  241. {
  242. int err = 0;
  243. if (atomic_dec_and_test(&io_end->count)) {
  244. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  245. err = ext4_convert_unwritten_extents(io_end->handle,
  246. io_end->inode, io_end->offset,
  247. io_end->size);
  248. io_end->handle = NULL;
  249. ext4_clear_io_unwritten_flag(io_end);
  250. }
  251. ext4_release_io_end(io_end);
  252. }
  253. return err;
  254. }
  255. ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
  256. {
  257. atomic_inc(&io_end->count);
  258. return io_end;
  259. }
  260. /* BIO completion function for page writeback */
  261. static void ext4_end_bio(struct bio *bio, int error)
  262. {
  263. ext4_io_end_t *io_end = bio->bi_private;
  264. sector_t bi_sector = bio->bi_sector;
  265. BUG_ON(!io_end);
  266. bio->bi_end_io = NULL;
  267. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  268. error = 0;
  269. if (error) {
  270. struct inode *inode = io_end->inode;
  271. ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
  272. "(offset %llu size %ld starting block %llu)",
  273. inode->i_ino,
  274. (unsigned long long) io_end->offset,
  275. (long) io_end->size,
  276. (unsigned long long)
  277. bi_sector >> (inode->i_blkbits - 9));
  278. }
  279. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  280. /*
  281. * Link bio into list hanging from io_end. We have to do it
  282. * atomically as bio completions can be racing against each
  283. * other.
  284. */
  285. bio->bi_private = xchg(&io_end->bio, bio);
  286. ext4_put_io_end_defer(io_end);
  287. } else {
  288. /*
  289. * Drop io_end reference early. Inode can get freed once
  290. * we finish the bio.
  291. */
  292. ext4_put_io_end_defer(io_end);
  293. ext4_finish_bio(bio);
  294. bio_put(bio);
  295. }
  296. }
  297. void ext4_io_submit(struct ext4_io_submit *io)
  298. {
  299. struct bio *bio = io->io_bio;
  300. if (bio) {
  301. bio_get(io->io_bio);
  302. submit_bio(io->io_op, io->io_bio);
  303. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  304. bio_put(io->io_bio);
  305. }
  306. io->io_bio = NULL;
  307. }
  308. void ext4_io_submit_init(struct ext4_io_submit *io,
  309. struct writeback_control *wbc)
  310. {
  311. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  312. io->io_bio = NULL;
  313. io->io_end = NULL;
  314. }
  315. static int io_submit_init_bio(struct ext4_io_submit *io,
  316. struct buffer_head *bh)
  317. {
  318. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  319. struct bio *bio;
  320. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  321. if (!bio)
  322. return -ENOMEM;
  323. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  324. bio->bi_bdev = bh->b_bdev;
  325. bio->bi_end_io = ext4_end_bio;
  326. bio->bi_private = ext4_get_io_end(io->io_end);
  327. io->io_bio = bio;
  328. io->io_next_block = bh->b_blocknr;
  329. return 0;
  330. }
  331. static int io_submit_add_bh(struct ext4_io_submit *io,
  332. struct inode *inode,
  333. struct buffer_head *bh)
  334. {
  335. int ret;
  336. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  337. submit_and_retry:
  338. ext4_io_submit(io);
  339. }
  340. if (io->io_bio == NULL) {
  341. ret = io_submit_init_bio(io, bh);
  342. if (ret)
  343. return ret;
  344. }
  345. ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
  346. if (ret != bh->b_size)
  347. goto submit_and_retry;
  348. io->io_next_block++;
  349. return 0;
  350. }
  351. int ext4_bio_write_page(struct ext4_io_submit *io,
  352. struct page *page,
  353. int len,
  354. struct writeback_control *wbc)
  355. {
  356. struct inode *inode = page->mapping->host;
  357. unsigned block_start, blocksize;
  358. struct buffer_head *bh, *head;
  359. int ret = 0;
  360. int nr_submitted = 0;
  361. blocksize = 1 << inode->i_blkbits;
  362. BUG_ON(!PageLocked(page));
  363. BUG_ON(PageWriteback(page));
  364. set_page_writeback(page);
  365. ClearPageError(page);
  366. /*
  367. * In the first loop we prepare and mark buffers to submit. We have to
  368. * mark all buffers in the page before submitting so that
  369. * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
  370. * on the first buffer finishes and we are still working on submitting
  371. * the second buffer.
  372. */
  373. bh = head = page_buffers(page);
  374. do {
  375. block_start = bh_offset(bh);
  376. if (block_start >= len) {
  377. /*
  378. * Comments copied from block_write_full_page_endio:
  379. *
  380. * The page straddles i_size. It must be zeroed out on
  381. * each and every writepage invocation because it may
  382. * be mmapped. "A file is mapped in multiples of the
  383. * page size. For a file that is not a multiple of
  384. * the page size, the remaining memory is zeroed when
  385. * mapped, and writes to that region are not written
  386. * out to the file."
  387. */
  388. zero_user_segment(page, block_start,
  389. block_start + blocksize);
  390. clear_buffer_dirty(bh);
  391. set_buffer_uptodate(bh);
  392. continue;
  393. }
  394. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  395. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  396. /* A hole? We can safely clear the dirty bit */
  397. if (!buffer_mapped(bh))
  398. clear_buffer_dirty(bh);
  399. if (io->io_bio)
  400. ext4_io_submit(io);
  401. continue;
  402. }
  403. if (buffer_new(bh)) {
  404. clear_buffer_new(bh);
  405. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  406. }
  407. set_buffer_async_write(bh);
  408. } while ((bh = bh->b_this_page) != head);
  409. /* Now submit buffers to write */
  410. bh = head = page_buffers(page);
  411. do {
  412. if (!buffer_async_write(bh))
  413. continue;
  414. ret = io_submit_add_bh(io, inode, bh);
  415. if (ret) {
  416. /*
  417. * We only get here on ENOMEM. Not much else
  418. * we can do but mark the page as dirty, and
  419. * better luck next time.
  420. */
  421. redirty_page_for_writepage(wbc, page);
  422. break;
  423. }
  424. nr_submitted++;
  425. clear_buffer_dirty(bh);
  426. } while ((bh = bh->b_this_page) != head);
  427. /* Error stopped previous loop? Clean up buffers... */
  428. if (ret) {
  429. do {
  430. clear_buffer_async_write(bh);
  431. bh = bh->b_this_page;
  432. } while (bh != head);
  433. }
  434. unlock_page(page);
  435. /* Nothing submitted - we have to end page writeback */
  436. if (!nr_submitted)
  437. end_page_writeback(page);
  438. return ret;
  439. }