page-io.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. struct bio_vec *bvec;
  58. bio_for_each_segment_all(bvec, bio, i) {
  59. struct page *page = bvec->bv_page;
  60. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  61. struct page *data_page = NULL;
  62. struct ext4_crypto_ctx *ctx = NULL;
  63. #endif
  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. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  72. if (!page->mapping) {
  73. /* The bounce data pages are unmapped. */
  74. data_page = page;
  75. ctx = (struct ext4_crypto_ctx *)page_private(data_page);
  76. page = ctx->w.control_page;
  77. }
  78. #endif
  79. if (bio->bi_error) {
  80. SetPageError(page);
  81. set_bit(AS_EIO, &page->mapping->flags);
  82. }
  83. bh = head = page_buffers(page);
  84. /*
  85. * We check all buffers in the page under BH_Uptodate_Lock
  86. * to avoid races with other end io clearing async_write flags
  87. */
  88. local_irq_save(flags);
  89. bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
  90. do {
  91. if (bh_offset(bh) < bio_start ||
  92. bh_offset(bh) + bh->b_size > bio_end) {
  93. if (buffer_async_write(bh))
  94. under_io++;
  95. continue;
  96. }
  97. clear_buffer_async_write(bh);
  98. if (bio->bi_error)
  99. buffer_io_error(bh);
  100. } while ((bh = bh->b_this_page) != head);
  101. bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
  102. local_irq_restore(flags);
  103. if (!under_io) {
  104. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  105. if (ctx)
  106. ext4_restore_control_page(data_page);
  107. #endif
  108. end_page_writeback(page);
  109. }
  110. }
  111. }
  112. static void ext4_release_io_end(ext4_io_end_t *io_end)
  113. {
  114. struct bio *bio, *next_bio;
  115. BUG_ON(!list_empty(&io_end->list));
  116. BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
  117. WARN_ON(io_end->handle);
  118. if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count))
  119. wake_up_all(ext4_ioend_wq(io_end->inode));
  120. for (bio = io_end->bio; bio; bio = next_bio) {
  121. next_bio = bio->bi_private;
  122. ext4_finish_bio(bio);
  123. bio_put(bio);
  124. }
  125. kmem_cache_free(io_end_cachep, io_end);
  126. }
  127. static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
  128. {
  129. struct inode *inode = io_end->inode;
  130. io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
  131. /* Wake up anyone waiting on unwritten extent conversion */
  132. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  133. wake_up_all(ext4_ioend_wq(inode));
  134. }
  135. /*
  136. * Check a range of space and convert unwritten extents to written. Note that
  137. * we are protected from truncate touching same part of extent tree by the
  138. * fact that truncate code waits for all DIO to finish (thus exclusion from
  139. * direct IO is achieved) and also waits for PageWriteback bits. Thus we
  140. * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
  141. * completed (happens from ext4_free_ioend()).
  142. */
  143. static int ext4_end_io(ext4_io_end_t *io)
  144. {
  145. struct inode *inode = io->inode;
  146. loff_t offset = io->offset;
  147. ssize_t size = io->size;
  148. handle_t *handle = io->handle;
  149. int ret = 0;
  150. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  151. "list->prev 0x%p\n",
  152. io, inode->i_ino, io->list.next, io->list.prev);
  153. io->handle = NULL; /* Following call will use up the handle */
  154. ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
  155. if (ret < 0) {
  156. ext4_msg(inode->i_sb, KERN_EMERG,
  157. "failed to convert unwritten extents to written "
  158. "extents -- potential data loss! "
  159. "(inode %lu, offset %llu, size %zd, error %d)",
  160. inode->i_ino, offset, size, ret);
  161. }
  162. ext4_clear_io_unwritten_flag(io);
  163. ext4_release_io_end(io);
  164. return ret;
  165. }
  166. static void dump_completed_IO(struct inode *inode, struct list_head *head)
  167. {
  168. #ifdef EXT4FS_DEBUG
  169. struct list_head *cur, *before, *after;
  170. ext4_io_end_t *io, *io0, *io1;
  171. if (list_empty(head))
  172. return;
  173. ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
  174. list_for_each_entry(io, head, list) {
  175. cur = &io->list;
  176. before = cur->prev;
  177. io0 = container_of(before, ext4_io_end_t, list);
  178. after = cur->next;
  179. io1 = container_of(after, ext4_io_end_t, list);
  180. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  181. io, inode->i_ino, io0, io1);
  182. }
  183. #endif
  184. }
  185. /* Add the io_end to per-inode completed end_io list. */
  186. static void ext4_add_complete_io(ext4_io_end_t *io_end)
  187. {
  188. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  189. struct ext4_sb_info *sbi = EXT4_SB(io_end->inode->i_sb);
  190. struct workqueue_struct *wq;
  191. unsigned long flags;
  192. /* Only reserved conversions from writeback should enter here */
  193. WARN_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  194. WARN_ON(!io_end->handle && sbi->s_journal);
  195. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  196. wq = sbi->rsv_conversion_wq;
  197. if (list_empty(&ei->i_rsv_conversion_list))
  198. queue_work(wq, &ei->i_rsv_conversion_work);
  199. list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
  200. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  201. }
  202. static int ext4_do_flush_completed_IO(struct inode *inode,
  203. struct list_head *head)
  204. {
  205. ext4_io_end_t *io;
  206. struct list_head unwritten;
  207. unsigned long flags;
  208. struct ext4_inode_info *ei = EXT4_I(inode);
  209. int err, ret = 0;
  210. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  211. dump_completed_IO(inode, head);
  212. list_replace_init(head, &unwritten);
  213. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  214. while (!list_empty(&unwritten)) {
  215. io = list_entry(unwritten.next, ext4_io_end_t, list);
  216. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  217. list_del_init(&io->list);
  218. err = ext4_end_io(io);
  219. if (unlikely(!ret && err))
  220. ret = err;
  221. }
  222. return ret;
  223. }
  224. /*
  225. * work on completed IO, to convert unwritten extents to extents
  226. */
  227. void ext4_end_io_rsv_work(struct work_struct *work)
  228. {
  229. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  230. i_rsv_conversion_work);
  231. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
  232. }
  233. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  234. {
  235. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  236. if (io) {
  237. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  238. io->inode = inode;
  239. INIT_LIST_HEAD(&io->list);
  240. atomic_set(&io->count, 1);
  241. }
  242. return io;
  243. }
  244. void ext4_put_io_end_defer(ext4_io_end_t *io_end)
  245. {
  246. if (atomic_dec_and_test(&io_end->count)) {
  247. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
  248. ext4_release_io_end(io_end);
  249. return;
  250. }
  251. ext4_add_complete_io(io_end);
  252. }
  253. }
  254. int ext4_put_io_end(ext4_io_end_t *io_end)
  255. {
  256. int err = 0;
  257. if (atomic_dec_and_test(&io_end->count)) {
  258. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  259. err = ext4_convert_unwritten_extents(io_end->handle,
  260. io_end->inode, io_end->offset,
  261. io_end->size);
  262. io_end->handle = NULL;
  263. ext4_clear_io_unwritten_flag(io_end);
  264. }
  265. ext4_release_io_end(io_end);
  266. }
  267. return err;
  268. }
  269. ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
  270. {
  271. atomic_inc(&io_end->count);
  272. return io_end;
  273. }
  274. /* BIO completion function for page writeback */
  275. static void ext4_end_bio(struct bio *bio)
  276. {
  277. ext4_io_end_t *io_end = bio->bi_private;
  278. sector_t bi_sector = bio->bi_iter.bi_sector;
  279. BUG_ON(!io_end);
  280. bio->bi_end_io = NULL;
  281. if (bio->bi_error) {
  282. struct inode *inode = io_end->inode;
  283. ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
  284. "(offset %llu size %ld starting block %llu)",
  285. bio->bi_error, inode->i_ino,
  286. (unsigned long long) io_end->offset,
  287. (long) io_end->size,
  288. (unsigned long long)
  289. bi_sector >> (inode->i_blkbits - 9));
  290. mapping_set_error(inode->i_mapping, bio->bi_error);
  291. }
  292. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  293. /*
  294. * Link bio into list hanging from io_end. We have to do it
  295. * atomically as bio completions can be racing against each
  296. * other.
  297. */
  298. bio->bi_private = xchg(&io_end->bio, bio);
  299. ext4_put_io_end_defer(io_end);
  300. } else {
  301. /*
  302. * Drop io_end reference early. Inode can get freed once
  303. * we finish the bio.
  304. */
  305. ext4_put_io_end_defer(io_end);
  306. ext4_finish_bio(bio);
  307. bio_put(bio);
  308. }
  309. }
  310. void ext4_io_submit(struct ext4_io_submit *io)
  311. {
  312. struct bio *bio = io->io_bio;
  313. if (bio) {
  314. int io_op = io->io_wbc->sync_mode == WB_SYNC_ALL ?
  315. WRITE_SYNC : WRITE;
  316. bio_get(io->io_bio);
  317. submit_bio(io_op, io->io_bio);
  318. bio_put(io->io_bio);
  319. }
  320. io->io_bio = NULL;
  321. }
  322. void ext4_io_submit_init(struct ext4_io_submit *io,
  323. struct writeback_control *wbc)
  324. {
  325. io->io_wbc = wbc;
  326. io->io_bio = NULL;
  327. io->io_end = NULL;
  328. }
  329. static int io_submit_init_bio(struct ext4_io_submit *io,
  330. struct buffer_head *bh)
  331. {
  332. struct bio *bio;
  333. bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
  334. if (!bio)
  335. return -ENOMEM;
  336. wbc_init_bio(io->io_wbc, bio);
  337. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  338. bio->bi_bdev = bh->b_bdev;
  339. bio->bi_end_io = ext4_end_bio;
  340. bio->bi_private = ext4_get_io_end(io->io_end);
  341. io->io_bio = bio;
  342. io->io_next_block = bh->b_blocknr;
  343. return 0;
  344. }
  345. static int io_submit_add_bh(struct ext4_io_submit *io,
  346. struct inode *inode,
  347. struct page *page,
  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, page, bh->b_size, bh_offset(bh));
  361. if (ret != bh->b_size)
  362. goto submit_and_retry;
  363. wbc_account_io(io->io_wbc, page, bh->b_size);
  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. }