page-io.c 12 KB

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