page-io.c 10 KB

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