page-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. if (io_end->flag & EXT4_IO_END_DIRECT)
  113. inode_dio_done(io_end->inode);
  114. if (io_end->iocb)
  115. aio_complete(io_end->iocb, io_end->result, 0);
  116. kmem_cache_free(io_end_cachep, io_end);
  117. }
  118. static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
  119. {
  120. struct inode *inode = io_end->inode;
  121. io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
  122. /* Wake up anyone waiting on unwritten extent conversion */
  123. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  124. wake_up_all(ext4_ioend_wq(inode));
  125. }
  126. /*
  127. * Check a range of space and convert unwritten extents to written. Note that
  128. * we are protected from truncate touching same part of extent tree by the
  129. * fact that truncate code waits for all DIO to finish (thus exclusion from
  130. * direct IO is achieved) and also waits for PageWriteback bits. Thus we
  131. * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
  132. * completed (happens from ext4_free_ioend()).
  133. */
  134. static int ext4_end_io(ext4_io_end_t *io)
  135. {
  136. struct inode *inode = io->inode;
  137. loff_t offset = io->offset;
  138. ssize_t size = io->size;
  139. handle_t *handle = io->handle;
  140. int ret = 0;
  141. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  142. "list->prev 0x%p\n",
  143. io, inode->i_ino, io->list.next, io->list.prev);
  144. io->handle = NULL; /* Following call will use up the handle */
  145. ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
  146. if (ret < 0) {
  147. ext4_msg(inode->i_sb, KERN_EMERG,
  148. "failed to convert unwritten extents to written "
  149. "extents -- potential data loss! "
  150. "(inode %lu, offset %llu, size %zd, error %d)",
  151. inode->i_ino, offset, size, ret);
  152. }
  153. ext4_clear_io_unwritten_flag(io);
  154. ext4_release_io_end(io);
  155. return ret;
  156. }
  157. static void dump_completed_IO(struct inode *inode, struct list_head *head)
  158. {
  159. #ifdef EXT4FS_DEBUG
  160. struct list_head *cur, *before, *after;
  161. ext4_io_end_t *io, *io0, *io1;
  162. if (list_empty(head))
  163. return;
  164. ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
  165. list_for_each_entry(io, head, list) {
  166. cur = &io->list;
  167. before = cur->prev;
  168. io0 = container_of(before, ext4_io_end_t, list);
  169. after = cur->next;
  170. io1 = container_of(after, ext4_io_end_t, list);
  171. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  172. io, inode->i_ino, io0, io1);
  173. }
  174. #endif
  175. }
  176. /* Add the io_end to per-inode completed end_io list. */
  177. static void ext4_add_complete_io(ext4_io_end_t *io_end)
  178. {
  179. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  180. struct workqueue_struct *wq;
  181. unsigned long flags;
  182. BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  183. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  184. if (io_end->handle) {
  185. wq = EXT4_SB(io_end->inode->i_sb)->rsv_conversion_wq;
  186. if (list_empty(&ei->i_rsv_conversion_list))
  187. queue_work(wq, &ei->i_rsv_conversion_work);
  188. list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
  189. } else {
  190. wq = EXT4_SB(io_end->inode->i_sb)->unrsv_conversion_wq;
  191. if (list_empty(&ei->i_unrsv_conversion_list))
  192. queue_work(wq, &ei->i_unrsv_conversion_work);
  193. list_add_tail(&io_end->list, &ei->i_unrsv_conversion_list);
  194. }
  195. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  196. }
  197. static int ext4_do_flush_completed_IO(struct inode *inode,
  198. struct list_head *head)
  199. {
  200. ext4_io_end_t *io;
  201. struct list_head unwritten;
  202. unsigned long flags;
  203. struct ext4_inode_info *ei = EXT4_I(inode);
  204. int err, ret = 0;
  205. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  206. dump_completed_IO(inode, head);
  207. list_replace_init(head, &unwritten);
  208. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  209. while (!list_empty(&unwritten)) {
  210. io = list_entry(unwritten.next, ext4_io_end_t, list);
  211. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  212. list_del_init(&io->list);
  213. err = ext4_end_io(io);
  214. if (unlikely(!ret && err))
  215. ret = err;
  216. }
  217. return ret;
  218. }
  219. /*
  220. * work on completed IO, to convert unwritten extents to extents
  221. */
  222. void ext4_end_io_rsv_work(struct work_struct *work)
  223. {
  224. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  225. i_rsv_conversion_work);
  226. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
  227. }
  228. void ext4_end_io_unrsv_work(struct work_struct *work)
  229. {
  230. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  231. i_unrsv_conversion_work);
  232. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_unrsv_conversion_list);
  233. }
  234. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  235. {
  236. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  237. if (io) {
  238. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  239. io->inode = inode;
  240. INIT_LIST_HEAD(&io->list);
  241. atomic_set(&io->count, 1);
  242. }
  243. return io;
  244. }
  245. void ext4_put_io_end_defer(ext4_io_end_t *io_end)
  246. {
  247. if (atomic_dec_and_test(&io_end->count)) {
  248. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
  249. ext4_release_io_end(io_end);
  250. return;
  251. }
  252. ext4_add_complete_io(io_end);
  253. }
  254. }
  255. int ext4_put_io_end(ext4_io_end_t *io_end)
  256. {
  257. int err = 0;
  258. if (atomic_dec_and_test(&io_end->count)) {
  259. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  260. err = ext4_convert_unwritten_extents(io_end->handle,
  261. io_end->inode, io_end->offset,
  262. io_end->size);
  263. io_end->handle = NULL;
  264. ext4_clear_io_unwritten_flag(io_end);
  265. }
  266. ext4_release_io_end(io_end);
  267. }
  268. return err;
  269. }
  270. ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
  271. {
  272. atomic_inc(&io_end->count);
  273. return io_end;
  274. }
  275. /* BIO completion function for page writeback */
  276. static void ext4_end_bio(struct bio *bio, int error)
  277. {
  278. ext4_io_end_t *io_end = bio->bi_private;
  279. sector_t bi_sector = bio->bi_sector;
  280. BUG_ON(!io_end);
  281. bio->bi_end_io = NULL;
  282. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  283. error = 0;
  284. if (error) {
  285. struct inode *inode = io_end->inode;
  286. ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
  287. "(offset %llu size %ld starting block %llu)",
  288. inode->i_ino,
  289. (unsigned long long) io_end->offset,
  290. (long) io_end->size,
  291. (unsigned long long)
  292. bi_sector >> (inode->i_blkbits - 9));
  293. }
  294. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  295. /*
  296. * Link bio into list hanging from io_end. We have to do it
  297. * atomically as bio completions can be racing against each
  298. * other.
  299. */
  300. bio->bi_private = xchg(&io_end->bio, bio);
  301. ext4_put_io_end_defer(io_end);
  302. } else {
  303. /*
  304. * Drop io_end reference early. Inode can get freed once
  305. * we finish the bio.
  306. */
  307. ext4_put_io_end_defer(io_end);
  308. ext4_finish_bio(bio);
  309. bio_put(bio);
  310. }
  311. }
  312. void ext4_io_submit(struct ext4_io_submit *io)
  313. {
  314. struct bio *bio = io->io_bio;
  315. if (bio) {
  316. bio_get(io->io_bio);
  317. submit_bio(io->io_op, io->io_bio);
  318. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  319. bio_put(io->io_bio);
  320. }
  321. io->io_bio = NULL;
  322. }
  323. void ext4_io_submit_init(struct ext4_io_submit *io,
  324. struct writeback_control *wbc)
  325. {
  326. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  327. io->io_bio = NULL;
  328. io->io_end = NULL;
  329. }
  330. static int io_submit_init_bio(struct ext4_io_submit *io,
  331. struct buffer_head *bh)
  332. {
  333. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  334. struct bio *bio;
  335. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  336. if (!bio)
  337. return -ENOMEM;
  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. }