page-io.c 11 KB

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