page-io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/uio.h>
  21. #include <linux/bio.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/mm.h>
  26. #include "ext4_jbd2.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. static struct kmem_cache *io_page_cachep, *io_end_cachep;
  30. int __init ext4_init_pageio(void)
  31. {
  32. io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
  33. if (io_page_cachep == NULL)
  34. return -ENOMEM;
  35. io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
  36. if (io_end_cachep == NULL) {
  37. kmem_cache_destroy(io_page_cachep);
  38. return -ENOMEM;
  39. }
  40. return 0;
  41. }
  42. void ext4_exit_pageio(void)
  43. {
  44. kmem_cache_destroy(io_end_cachep);
  45. kmem_cache_destroy(io_page_cachep);
  46. }
  47. /*
  48. * This function is called by ext4_evict_inode() to make sure there is
  49. * no more pending I/O completion work left to do.
  50. */
  51. void ext4_ioend_shutdown(struct inode *inode)
  52. {
  53. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  54. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
  55. /*
  56. * We need to make sure the work structure is finished being
  57. * used before we let the inode get destroyed.
  58. */
  59. if (work_pending(&EXT4_I(inode)->i_unwritten_work))
  60. cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
  61. }
  62. static void put_io_page(struct ext4_io_page *io_page)
  63. {
  64. if (atomic_dec_and_test(&io_page->p_count)) {
  65. end_page_writeback(io_page->p_page);
  66. put_page(io_page->p_page);
  67. kmem_cache_free(io_page_cachep, io_page);
  68. }
  69. }
  70. void ext4_free_io_end(ext4_io_end_t *io)
  71. {
  72. int i;
  73. BUG_ON(!io);
  74. BUG_ON(!list_empty(&io->list));
  75. BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
  76. for (i = 0; i < io->num_io_pages; i++)
  77. put_io_page(io->pages[i]);
  78. io->num_io_pages = 0;
  79. if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
  80. wake_up_all(ext4_ioend_wq(io->inode));
  81. kmem_cache_free(io_end_cachep, io);
  82. }
  83. /* check a range of space and convert unwritten extents to written. */
  84. static int ext4_end_io(ext4_io_end_t *io)
  85. {
  86. struct inode *inode = io->inode;
  87. loff_t offset = io->offset;
  88. ssize_t size = io->size;
  89. int ret = 0;
  90. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  91. "list->prev 0x%p\n",
  92. io, inode->i_ino, io->list.next, io->list.prev);
  93. ret = ext4_convert_unwritten_extents(inode, offset, size);
  94. if (ret < 0) {
  95. ext4_msg(inode->i_sb, KERN_EMERG,
  96. "failed to convert unwritten extents to written "
  97. "extents -- potential data loss! "
  98. "(inode %lu, offset %llu, size %zd, error %d)",
  99. inode->i_ino, offset, size, ret);
  100. }
  101. /* Wake up anyone waiting on unwritten extent conversion */
  102. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  103. wake_up_all(ext4_ioend_wq(inode));
  104. if (io->flag & EXT4_IO_END_DIRECT)
  105. inode_dio_done(inode);
  106. if (io->iocb)
  107. aio_complete(io->iocb, io->result, 0);
  108. return ret;
  109. }
  110. static void dump_completed_IO(struct inode *inode)
  111. {
  112. #ifdef EXT4FS_DEBUG
  113. struct list_head *cur, *before, *after;
  114. ext4_io_end_t *io, *io0, *io1;
  115. if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
  116. ext4_debug("inode %lu completed_io list is empty\n",
  117. inode->i_ino);
  118. return;
  119. }
  120. ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
  121. list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
  122. cur = &io->list;
  123. before = cur->prev;
  124. io0 = container_of(before, ext4_io_end_t, list);
  125. after = cur->next;
  126. io1 = container_of(after, ext4_io_end_t, list);
  127. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  128. io, inode->i_ino, io0, io1);
  129. }
  130. #endif
  131. }
  132. /* Add the io_end to per-inode completed end_io list. */
  133. void ext4_add_complete_io(ext4_io_end_t *io_end)
  134. {
  135. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  136. struct workqueue_struct *wq;
  137. unsigned long flags;
  138. BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  139. wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
  140. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  141. if (list_empty(&ei->i_completed_io_list))
  142. queue_work(wq, &ei->i_unwritten_work);
  143. list_add_tail(&io_end->list, &ei->i_completed_io_list);
  144. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  145. }
  146. static int ext4_do_flush_completed_IO(struct inode *inode)
  147. {
  148. ext4_io_end_t *io;
  149. struct list_head unwritten;
  150. unsigned long flags;
  151. struct ext4_inode_info *ei = EXT4_I(inode);
  152. int err, ret = 0;
  153. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  154. dump_completed_IO(inode);
  155. list_replace_init(&ei->i_completed_io_list, &unwritten);
  156. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  157. while (!list_empty(&unwritten)) {
  158. io = list_entry(unwritten.next, ext4_io_end_t, list);
  159. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  160. list_del_init(&io->list);
  161. err = ext4_end_io(io);
  162. if (unlikely(!ret && err))
  163. ret = err;
  164. io->flag &= ~EXT4_IO_END_UNWRITTEN;
  165. ext4_free_io_end(io);
  166. }
  167. return ret;
  168. }
  169. /*
  170. * work on completed aio dio IO, to convert unwritten extents to extents
  171. */
  172. void ext4_end_io_work(struct work_struct *work)
  173. {
  174. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  175. i_unwritten_work);
  176. ext4_do_flush_completed_IO(&ei->vfs_inode);
  177. }
  178. int ext4_flush_unwritten_io(struct inode *inode)
  179. {
  180. int ret;
  181. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
  182. !(inode->i_state & I_FREEING));
  183. ret = ext4_do_flush_completed_IO(inode);
  184. ext4_unwritten_wait(inode);
  185. return ret;
  186. }
  187. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  188. {
  189. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  190. if (io) {
  191. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  192. io->inode = inode;
  193. INIT_LIST_HEAD(&io->list);
  194. }
  195. return io;
  196. }
  197. /*
  198. * Print an buffer I/O error compatible with the fs/buffer.c. This
  199. * provides compatibility with dmesg scrapers that look for a specific
  200. * buffer I/O error message. We really need a unified error reporting
  201. * structure to userspace ala Digital Unix's uerf system, but it's
  202. * probably not going to happen in my lifetime, due to LKML politics...
  203. */
  204. static void buffer_io_error(struct buffer_head *bh)
  205. {
  206. char b[BDEVNAME_SIZE];
  207. printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
  208. bdevname(bh->b_bdev, b),
  209. (unsigned long long)bh->b_blocknr);
  210. }
  211. static void ext4_end_bio(struct bio *bio, int error)
  212. {
  213. ext4_io_end_t *io_end = bio->bi_private;
  214. struct inode *inode;
  215. int i;
  216. sector_t bi_sector = bio->bi_sector;
  217. BUG_ON(!io_end);
  218. bio->bi_private = NULL;
  219. bio->bi_end_io = NULL;
  220. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  221. error = 0;
  222. bio_put(bio);
  223. for (i = 0; i < io_end->num_io_pages; i++) {
  224. struct page *page = io_end->pages[i]->p_page;
  225. struct buffer_head *bh, *head;
  226. loff_t offset;
  227. loff_t io_end_offset;
  228. if (error) {
  229. SetPageError(page);
  230. set_bit(AS_EIO, &page->mapping->flags);
  231. head = page_buffers(page);
  232. BUG_ON(!head);
  233. io_end_offset = io_end->offset + io_end->size;
  234. offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
  235. bh = head;
  236. do {
  237. if ((offset >= io_end->offset) &&
  238. (offset+bh->b_size <= io_end_offset))
  239. buffer_io_error(bh);
  240. offset += bh->b_size;
  241. bh = bh->b_this_page;
  242. } while (bh != head);
  243. }
  244. put_io_page(io_end->pages[i]);
  245. }
  246. io_end->num_io_pages = 0;
  247. inode = io_end->inode;
  248. if (error) {
  249. io_end->flag |= EXT4_IO_END_ERROR;
  250. ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
  251. "(offset %llu size %ld starting block %llu)",
  252. inode->i_ino,
  253. (unsigned long long) io_end->offset,
  254. (long) io_end->size,
  255. (unsigned long long)
  256. bi_sector >> (inode->i_blkbits - 9));
  257. }
  258. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  259. ext4_free_io_end(io_end);
  260. return;
  261. }
  262. ext4_add_complete_io(io_end);
  263. }
  264. void ext4_io_submit(struct ext4_io_submit *io)
  265. {
  266. struct bio *bio = io->io_bio;
  267. if (bio) {
  268. bio_get(io->io_bio);
  269. submit_bio(io->io_op, io->io_bio);
  270. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  271. bio_put(io->io_bio);
  272. }
  273. io->io_bio = NULL;
  274. io->io_op = 0;
  275. io->io_end = NULL;
  276. }
  277. static int io_submit_init(struct ext4_io_submit *io,
  278. struct inode *inode,
  279. struct writeback_control *wbc,
  280. struct buffer_head *bh)
  281. {
  282. ext4_io_end_t *io_end;
  283. struct page *page = bh->b_page;
  284. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  285. struct bio *bio;
  286. io_end = ext4_init_io_end(inode, GFP_NOFS);
  287. if (!io_end)
  288. return -ENOMEM;
  289. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  290. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  291. bio->bi_bdev = bh->b_bdev;
  292. bio->bi_private = io->io_end = io_end;
  293. bio->bi_end_io = ext4_end_bio;
  294. io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
  295. io->io_bio = bio;
  296. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  297. io->io_next_block = bh->b_blocknr;
  298. return 0;
  299. }
  300. static int io_submit_add_bh(struct ext4_io_submit *io,
  301. struct ext4_io_page *io_page,
  302. struct inode *inode,
  303. struct writeback_control *wbc,
  304. struct buffer_head *bh)
  305. {
  306. ext4_io_end_t *io_end;
  307. int ret;
  308. if (buffer_new(bh)) {
  309. clear_buffer_new(bh);
  310. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  311. }
  312. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  313. submit_and_retry:
  314. ext4_io_submit(io);
  315. }
  316. if (io->io_bio == NULL) {
  317. ret = io_submit_init(io, inode, wbc, bh);
  318. if (ret)
  319. return ret;
  320. }
  321. io_end = io->io_end;
  322. if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
  323. (io_end->pages[io_end->num_io_pages-1] != io_page))
  324. goto submit_and_retry;
  325. if (buffer_uninit(bh))
  326. ext4_set_io_unwritten_flag(inode, io_end);
  327. io->io_end->size += bh->b_size;
  328. io->io_next_block++;
  329. ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
  330. if (ret != bh->b_size)
  331. goto submit_and_retry;
  332. if ((io_end->num_io_pages == 0) ||
  333. (io_end->pages[io_end->num_io_pages-1] != io_page)) {
  334. io_end->pages[io_end->num_io_pages++] = io_page;
  335. atomic_inc(&io_page->p_count);
  336. }
  337. return 0;
  338. }
  339. int ext4_bio_write_page(struct ext4_io_submit *io,
  340. struct page *page,
  341. int len,
  342. struct writeback_control *wbc)
  343. {
  344. struct inode *inode = page->mapping->host;
  345. unsigned block_start, block_end, blocksize;
  346. struct ext4_io_page *io_page;
  347. struct buffer_head *bh, *head;
  348. int ret = 0;
  349. blocksize = 1 << inode->i_blkbits;
  350. BUG_ON(!PageLocked(page));
  351. BUG_ON(PageWriteback(page));
  352. io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
  353. if (!io_page) {
  354. redirty_page_for_writepage(wbc, page);
  355. unlock_page(page);
  356. return -ENOMEM;
  357. }
  358. io_page->p_page = page;
  359. atomic_set(&io_page->p_count, 1);
  360. get_page(page);
  361. set_page_writeback(page);
  362. ClearPageError(page);
  363. for (bh = head = page_buffers(page), block_start = 0;
  364. bh != head || !block_start;
  365. block_start = block_end, bh = bh->b_this_page) {
  366. block_end = block_start + blocksize;
  367. if (block_start >= len) {
  368. /*
  369. * Comments copied from block_write_full_page_endio:
  370. *
  371. * The page straddles i_size. It must be zeroed out on
  372. * each and every writepage invocation because it may
  373. * be mmapped. "A file is mapped in multiples of the
  374. * page size. For a file that is not a multiple of
  375. * the page size, the remaining memory is zeroed when
  376. * mapped, and writes to that region are not written
  377. * out to the file."
  378. */
  379. zero_user_segment(page, block_start, block_end);
  380. clear_buffer_dirty(bh);
  381. set_buffer_uptodate(bh);
  382. continue;
  383. }
  384. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  385. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  386. /* A hole? We can safely clear the dirty bit */
  387. if (!buffer_mapped(bh))
  388. clear_buffer_dirty(bh);
  389. if (io->io_bio)
  390. ext4_io_submit(io);
  391. continue;
  392. }
  393. ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
  394. if (ret) {
  395. /*
  396. * We only get here on ENOMEM. Not much else
  397. * we can do but mark the page as dirty, and
  398. * better luck next time.
  399. */
  400. redirty_page_for_writepage(wbc, page);
  401. break;
  402. }
  403. clear_buffer_dirty(bh);
  404. }
  405. unlock_page(page);
  406. /*
  407. * If the page was truncated before we could do the writeback,
  408. * or we had a memory allocation error while trying to write
  409. * the first buffer head, we won't have submitted any pages for
  410. * I/O. In that case we need to make sure we've cleared the
  411. * PageWriteback bit from the page to prevent the system from
  412. * wedging later on.
  413. */
  414. put_io_page(io_page);
  415. return ret;
  416. }