page-io.c 14 KB

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