page-io.c 13 KB

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