page-io.c 11 KB

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