page-io.c 13 KB

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