page-io.c 11 KB

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