page-io.c 14 KB

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