page-io.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. char b[BDEVNAME_SIZE];
  50. printk_ratelimited(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
  51. bdevname(bh->b_bdev, b),
  52. (unsigned long long)bh->b_blocknr);
  53. }
  54. static void ext4_finish_bio(struct bio *bio)
  55. {
  56. int i;
  57. int error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
  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 (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 (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, int error)
  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 (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 %d writing to inode %lu "
  287. "(offset %llu size %ld starting block %llu)",
  288. error, 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. mapping_set_error(inode->i_mapping, error);
  294. }
  295. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  296. /*
  297. * Link bio into list hanging from io_end. We have to do it
  298. * atomically as bio completions can be racing against each
  299. * other.
  300. */
  301. bio->bi_private = xchg(&io_end->bio, bio);
  302. ext4_put_io_end_defer(io_end);
  303. } else {
  304. /*
  305. * Drop io_end reference early. Inode can get freed once
  306. * we finish the bio.
  307. */
  308. ext4_put_io_end_defer(io_end);
  309. ext4_finish_bio(bio);
  310. bio_put(bio);
  311. }
  312. }
  313. void ext4_io_submit(struct ext4_io_submit *io)
  314. {
  315. struct bio *bio = io->io_bio;
  316. if (bio) {
  317. bio_get(io->io_bio);
  318. submit_bio(io->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_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_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. io->io_next_block++;
  365. return 0;
  366. }
  367. int ext4_bio_write_page(struct ext4_io_submit *io,
  368. struct page *page,
  369. int len,
  370. struct writeback_control *wbc,
  371. bool keep_towrite)
  372. {
  373. struct page *data_page = NULL;
  374. struct inode *inode = page->mapping->host;
  375. unsigned block_start, blocksize;
  376. struct buffer_head *bh, *head;
  377. int ret = 0;
  378. int nr_submitted = 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. } while ((bh = bh->b_this_page) != head);
  428. bh = head = page_buffers(page);
  429. if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
  430. data_page = ext4_encrypt(inode, page);
  431. if (IS_ERR(data_page)) {
  432. ret = PTR_ERR(data_page);
  433. data_page = NULL;
  434. goto out;
  435. }
  436. }
  437. /* Now submit buffers to write */
  438. do {
  439. if (!buffer_async_write(bh))
  440. continue;
  441. ret = io_submit_add_bh(io, inode,
  442. data_page ? data_page : page, bh);
  443. if (ret) {
  444. /*
  445. * We only get here on ENOMEM. Not much else
  446. * we can do but mark the page as dirty, and
  447. * better luck next time.
  448. */
  449. break;
  450. }
  451. nr_submitted++;
  452. clear_buffer_dirty(bh);
  453. } while ((bh = bh->b_this_page) != head);
  454. /* Error stopped previous loop? Clean up buffers... */
  455. if (ret) {
  456. out:
  457. if (data_page)
  458. ext4_restore_control_page(data_page);
  459. printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
  460. redirty_page_for_writepage(wbc, page);
  461. do {
  462. clear_buffer_async_write(bh);
  463. bh = bh->b_this_page;
  464. } while (bh != head);
  465. }
  466. unlock_page(page);
  467. /* Nothing submitted - we have to end page writeback */
  468. if (!nr_submitted)
  469. end_page_writeback(page);
  470. return ret;
  471. }