page-io.c 14 KB

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