page-io.c 11 KB

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