page-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. static struct kmem_cache *io_page_cachep, *io_end_cachep;
  29. int __init ext4_init_pageio(void)
  30. {
  31. io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
  32. if (io_page_cachep == NULL)
  33. return -ENOMEM;
  34. io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
  35. if (io_end_cachep == NULL) {
  36. kmem_cache_destroy(io_page_cachep);
  37. return -ENOMEM;
  38. }
  39. return 0;
  40. }
  41. void ext4_exit_pageio(void)
  42. {
  43. kmem_cache_destroy(io_end_cachep);
  44. kmem_cache_destroy(io_page_cachep);
  45. }
  46. void ext4_ioend_wait(struct inode *inode)
  47. {
  48. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  49. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
  50. }
  51. static void put_io_page(struct ext4_io_page *io_page)
  52. {
  53. if (atomic_dec_and_test(&io_page->p_count)) {
  54. end_page_writeback(io_page->p_page);
  55. put_page(io_page->p_page);
  56. kmem_cache_free(io_page_cachep, io_page);
  57. }
  58. }
  59. void ext4_free_io_end(ext4_io_end_t *io)
  60. {
  61. int i;
  62. BUG_ON(!io);
  63. BUG_ON(!list_empty(&io->list));
  64. BUG_ON(io->flag & EXT4_IO_END_UNWRITTEN);
  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. /* check a range of space and convert unwritten extents to written. */
  75. static int ext4_end_io(ext4_io_end_t *io)
  76. {
  77. struct inode *inode = io->inode;
  78. loff_t offset = io->offset;
  79. ssize_t size = io->size;
  80. int ret = 0;
  81. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  82. "list->prev 0x%p\n",
  83. io, inode->i_ino, io->list.next, io->list.prev);
  84. ret = ext4_convert_unwritten_extents(inode, offset, size);
  85. if (ret < 0) {
  86. ext4_msg(inode->i_sb, KERN_EMERG,
  87. "failed to convert unwritten extents to written "
  88. "extents -- potential data loss! "
  89. "(inode %lu, offset %llu, size %zd, error %d)",
  90. inode->i_ino, offset, size, ret);
  91. }
  92. if (io->iocb)
  93. aio_complete(io->iocb, io->result, 0);
  94. if (io->flag & EXT4_IO_END_DIRECT)
  95. inode_dio_done(inode);
  96. /* Wake up anyone waiting on unwritten extent conversion */
  97. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  98. wake_up_all(ext4_ioend_wq(inode));
  99. return ret;
  100. }
  101. static void dump_completed_IO(struct inode *inode)
  102. {
  103. #ifdef EXT4FS_DEBUG
  104. struct list_head *cur, *before, *after;
  105. ext4_io_end_t *io, *io0, *io1;
  106. unsigned long flags;
  107. if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
  108. ext4_debug("inode %lu completed_io list is empty\n",
  109. inode->i_ino);
  110. return;
  111. }
  112. ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
  113. list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
  114. cur = &io->list;
  115. before = cur->prev;
  116. io0 = container_of(before, ext4_io_end_t, list);
  117. after = cur->next;
  118. io1 = container_of(after, ext4_io_end_t, list);
  119. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  120. io, inode->i_ino, io0, io1);
  121. }
  122. #endif
  123. }
  124. /* Add the io_end to per-inode completed end_io list. */
  125. void ext4_add_complete_io(ext4_io_end_t *io_end)
  126. {
  127. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  128. struct workqueue_struct *wq;
  129. unsigned long flags;
  130. BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  131. wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
  132. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  133. if (list_empty(&ei->i_completed_io_list)) {
  134. io_end->flag |= EXT4_IO_END_QUEUED;
  135. queue_work(wq, &io_end->work);
  136. }
  137. list_add_tail(&io_end->list, &ei->i_completed_io_list);
  138. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  139. }
  140. static int ext4_do_flush_completed_IO(struct inode *inode,
  141. ext4_io_end_t *work_io)
  142. {
  143. ext4_io_end_t *io;
  144. struct list_head unwritten, complete, to_free;
  145. unsigned long flags;
  146. struct ext4_inode_info *ei = EXT4_I(inode);
  147. int err, ret = 0;
  148. INIT_LIST_HEAD(&complete);
  149. INIT_LIST_HEAD(&to_free);
  150. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  151. dump_completed_IO(inode);
  152. list_replace_init(&ei->i_completed_io_list, &unwritten);
  153. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  154. while (!list_empty(&unwritten)) {
  155. io = list_entry(unwritten.next, ext4_io_end_t, list);
  156. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  157. list_del_init(&io->list);
  158. err = ext4_end_io(io);
  159. if (unlikely(!ret && err))
  160. ret = err;
  161. list_add_tail(&io->list, &complete);
  162. }
  163. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  164. while (!list_empty(&complete)) {
  165. io = list_entry(complete.next, ext4_io_end_t, list);
  166. io->flag &= ~EXT4_IO_END_UNWRITTEN;
  167. /* end_io context can not be destroyed now because it still
  168. * used by queued worker. Worker thread will destroy it later */
  169. if (io->flag & EXT4_IO_END_QUEUED)
  170. list_del_init(&io->list);
  171. else
  172. list_move(&io->list, &to_free);
  173. }
  174. /* If we are called from worker context, it is time to clear queued
  175. * flag, and destroy it's end_io if it was converted already */
  176. if (work_io) {
  177. work_io->flag &= ~EXT4_IO_END_QUEUED;
  178. if (!(work_io->flag & EXT4_IO_END_UNWRITTEN))
  179. list_add_tail(&work_io->list, &to_free);
  180. }
  181. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  182. while (!list_empty(&to_free)) {
  183. io = list_entry(to_free.next, ext4_io_end_t, list);
  184. list_del_init(&io->list);
  185. ext4_free_io_end(io);
  186. }
  187. return ret;
  188. }
  189. /*
  190. * work on completed aio dio IO, to convert unwritten extents to extents
  191. */
  192. static void ext4_end_io_work(struct work_struct *work)
  193. {
  194. ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
  195. ext4_do_flush_completed_IO(io->inode, io);
  196. }
  197. int ext4_flush_unwritten_io(struct inode *inode)
  198. {
  199. int ret;
  200. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
  201. !(inode->i_state & I_FREEING));
  202. ret = ext4_do_flush_completed_IO(inode, NULL);
  203. ext4_unwritten_wait(inode);
  204. return ret;
  205. }
  206. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  207. {
  208. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  209. if (io) {
  210. atomic_inc(&EXT4_I(inode)->i_ioend_count);
  211. io->inode = inode;
  212. INIT_WORK(&io->work, ext4_end_io_work);
  213. INIT_LIST_HEAD(&io->list);
  214. }
  215. return io;
  216. }
  217. /*
  218. * Print an buffer I/O error compatible with the fs/buffer.c. This
  219. * provides compatibility with dmesg scrapers that look for a specific
  220. * buffer I/O error message. We really need a unified error reporting
  221. * structure to userspace ala Digital Unix's uerf system, but it's
  222. * probably not going to happen in my lifetime, due to LKML politics...
  223. */
  224. static void buffer_io_error(struct buffer_head *bh)
  225. {
  226. char b[BDEVNAME_SIZE];
  227. printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
  228. bdevname(bh->b_bdev, b),
  229. (unsigned long long)bh->b_blocknr);
  230. }
  231. static void ext4_end_bio(struct bio *bio, int error)
  232. {
  233. ext4_io_end_t *io_end = bio->bi_private;
  234. struct inode *inode;
  235. int i;
  236. sector_t bi_sector = bio->bi_sector;
  237. BUG_ON(!io_end);
  238. bio->bi_private = NULL;
  239. bio->bi_end_io = NULL;
  240. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  241. error = 0;
  242. bio_put(bio);
  243. for (i = 0; i < io_end->num_io_pages; i++) {
  244. struct page *page = io_end->pages[i]->p_page;
  245. struct buffer_head *bh, *head;
  246. loff_t offset;
  247. loff_t io_end_offset;
  248. if (error) {
  249. SetPageError(page);
  250. set_bit(AS_EIO, &page->mapping->flags);
  251. head = page_buffers(page);
  252. BUG_ON(!head);
  253. io_end_offset = io_end->offset + io_end->size;
  254. offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
  255. bh = head;
  256. do {
  257. if ((offset >= io_end->offset) &&
  258. (offset+bh->b_size <= io_end_offset))
  259. buffer_io_error(bh);
  260. offset += bh->b_size;
  261. bh = bh->b_this_page;
  262. } while (bh != head);
  263. }
  264. put_io_page(io_end->pages[i]);
  265. }
  266. io_end->num_io_pages = 0;
  267. inode = io_end->inode;
  268. if (error) {
  269. io_end->flag |= EXT4_IO_END_ERROR;
  270. ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
  271. "(offset %llu size %ld starting block %llu)",
  272. inode->i_ino,
  273. (unsigned long long) io_end->offset,
  274. (long) io_end->size,
  275. (unsigned long long)
  276. bi_sector >> (inode->i_blkbits - 9));
  277. }
  278. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  279. ext4_free_io_end(io_end);
  280. return;
  281. }
  282. ext4_add_complete_io(io_end);
  283. }
  284. void ext4_io_submit(struct ext4_io_submit *io)
  285. {
  286. struct bio *bio = io->io_bio;
  287. if (bio) {
  288. bio_get(io->io_bio);
  289. submit_bio(io->io_op, io->io_bio);
  290. BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
  291. bio_put(io->io_bio);
  292. }
  293. io->io_bio = NULL;
  294. io->io_op = 0;
  295. io->io_end = NULL;
  296. }
  297. static int io_submit_init(struct ext4_io_submit *io,
  298. struct inode *inode,
  299. struct writeback_control *wbc,
  300. struct buffer_head *bh)
  301. {
  302. ext4_io_end_t *io_end;
  303. struct page *page = bh->b_page;
  304. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  305. struct bio *bio;
  306. io_end = ext4_init_io_end(inode, GFP_NOFS);
  307. if (!io_end)
  308. return -ENOMEM;
  309. bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
  310. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  311. bio->bi_bdev = bh->b_bdev;
  312. bio->bi_private = io->io_end = io_end;
  313. bio->bi_end_io = ext4_end_bio;
  314. io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
  315. io->io_bio = bio;
  316. io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
  317. io->io_next_block = bh->b_blocknr;
  318. return 0;
  319. }
  320. static int io_submit_add_bh(struct ext4_io_submit *io,
  321. struct ext4_io_page *io_page,
  322. struct inode *inode,
  323. struct writeback_control *wbc,
  324. struct buffer_head *bh)
  325. {
  326. ext4_io_end_t *io_end;
  327. int ret;
  328. if (buffer_new(bh)) {
  329. clear_buffer_new(bh);
  330. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  331. }
  332. if (!buffer_mapped(bh) || buffer_delay(bh)) {
  333. if (!buffer_mapped(bh))
  334. clear_buffer_dirty(bh);
  335. if (io->io_bio)
  336. ext4_io_submit(io);
  337. return 0;
  338. }
  339. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  340. submit_and_retry:
  341. ext4_io_submit(io);
  342. }
  343. if (io->io_bio == NULL) {
  344. ret = io_submit_init(io, inode, wbc, bh);
  345. if (ret)
  346. return ret;
  347. }
  348. io_end = io->io_end;
  349. if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
  350. (io_end->pages[io_end->num_io_pages-1] != io_page))
  351. goto submit_and_retry;
  352. if (buffer_uninit(bh))
  353. ext4_set_io_unwritten_flag(inode, io_end);
  354. io->io_end->size += bh->b_size;
  355. io->io_next_block++;
  356. ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
  357. if (ret != bh->b_size)
  358. goto submit_and_retry;
  359. if ((io_end->num_io_pages == 0) ||
  360. (io_end->pages[io_end->num_io_pages-1] != io_page)) {
  361. io_end->pages[io_end->num_io_pages++] = io_page;
  362. atomic_inc(&io_page->p_count);
  363. }
  364. return 0;
  365. }
  366. int ext4_bio_write_page(struct ext4_io_submit *io,
  367. struct page *page,
  368. int len,
  369. struct writeback_control *wbc)
  370. {
  371. struct inode *inode = page->mapping->host;
  372. unsigned block_start, block_end, blocksize;
  373. struct ext4_io_page *io_page;
  374. struct buffer_head *bh, *head;
  375. int ret = 0;
  376. blocksize = 1 << inode->i_blkbits;
  377. BUG_ON(!PageLocked(page));
  378. BUG_ON(PageWriteback(page));
  379. io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
  380. if (!io_page) {
  381. set_page_dirty(page);
  382. unlock_page(page);
  383. return -ENOMEM;
  384. }
  385. io_page->p_page = page;
  386. atomic_set(&io_page->p_count, 1);
  387. get_page(page);
  388. set_page_writeback(page);
  389. ClearPageError(page);
  390. for (bh = head = page_buffers(page), block_start = 0;
  391. bh != head || !block_start;
  392. block_start = block_end, bh = bh->b_this_page) {
  393. block_end = block_start + blocksize;
  394. if (block_start >= len) {
  395. /*
  396. * Comments copied from block_write_full_page_endio:
  397. *
  398. * The page straddles i_size. It must be zeroed out on
  399. * each and every writepage invocation because it may
  400. * be mmapped. "A file is mapped in multiples of the
  401. * page size. For a file that is not a multiple of
  402. * the page size, the remaining memory is zeroed when
  403. * mapped, and writes to that region are not written
  404. * out to the file."
  405. */
  406. zero_user_segment(page, block_start, block_end);
  407. clear_buffer_dirty(bh);
  408. set_buffer_uptodate(bh);
  409. continue;
  410. }
  411. clear_buffer_dirty(bh);
  412. ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
  413. if (ret) {
  414. /*
  415. * We only get here on ENOMEM. Not much else
  416. * we can do but mark the page as dirty, and
  417. * better luck next time.
  418. */
  419. set_page_dirty(page);
  420. break;
  421. }
  422. }
  423. unlock_page(page);
  424. /*
  425. * If the page was truncated before we could do the writeback,
  426. * or we had a memory allocation error while trying to write
  427. * the first buffer head, we won't have submitted any pages for
  428. * I/O. In that case we need to make sure we've cleared the
  429. * PageWriteback bit from the page to prevent the system from
  430. * wedging later on.
  431. */
  432. put_io_page(io_page);
  433. return ret;
  434. }