page-io.c 11 KB

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