PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext4/inode.c

https://github.com/mstsirkin/linux
C | 1733 lines | 1055 code | 176 blank | 502 comment | 242 complexity | 0c6e1b5ecd484b8efe753aaaf4eafee5 MD5 | raw file
  1. /*
  2. * linux/fs/ext4/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  16. * (jj@sunsite.ms.mff.cuni.cz)
  17. *
  18. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  19. */
  20. #include <linux/module.h>
  21. #include <linux/fs.h>
  22. #include <linux/time.h>
  23. #include <linux/jbd2.h>
  24. #include <linux/highuid.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/string.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/writeback.h>
  30. #include <linux/pagevec.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/uio.h>
  34. #include <linux/bio.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/kernel.h>
  37. #include <linux/printk.h>
  38. #include <linux/slab.h>
  39. #include <linux/ratelimit.h>
  40. #include "ext4_jbd2.h"
  41. #include "xattr.h"
  42. #include "acl.h"
  43. #include "ext4_extents.h"
  44. #include "truncate.h"
  45. #include <trace/events/ext4.h>
  46. #define MPAGE_DA_EXTENT_TAIL 0x01
  47. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  48. loff_t new_size)
  49. {
  50. trace_ext4_begin_ordered_truncate(inode, new_size);
  51. /*
  52. * If jinode is zero, then we never opened the file for
  53. * writing, so there's no need to call
  54. * jbd2_journal_begin_ordered_truncate() since there's no
  55. * outstanding writes we need to flush.
  56. */
  57. if (!EXT4_I(inode)->jinode)
  58. return 0;
  59. return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
  60. EXT4_I(inode)->jinode,
  61. new_size);
  62. }
  63. static void ext4_invalidatepage(struct page *page, unsigned long offset);
  64. static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
  65. struct buffer_head *bh_result, int create);
  66. static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
  67. static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
  68. static int __ext4_journalled_writepage(struct page *page, unsigned int len);
  69. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
  70. /*
  71. * Test whether an inode is a fast symlink.
  72. */
  73. static int ext4_inode_is_fast_symlink(struct inode *inode)
  74. {
  75. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  76. (inode->i_sb->s_blocksize >> 9) : 0;
  77. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  78. }
  79. /*
  80. * Restart the transaction associated with *handle. This does a commit,
  81. * so before we call here everything must be consistently dirtied against
  82. * this transaction.
  83. */
  84. int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
  85. int nblocks)
  86. {
  87. int ret;
  88. /*
  89. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  90. * moment, get_block can be called only for blocks inside i_size since
  91. * page cache has been already dropped and writes are blocked by
  92. * i_mutex. So we can safely drop the i_data_sem here.
  93. */
  94. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  95. jbd_debug(2, "restarting handle %p\n", handle);
  96. up_write(&EXT4_I(inode)->i_data_sem);
  97. ret = ext4_journal_restart(handle, nblocks);
  98. down_write(&EXT4_I(inode)->i_data_sem);
  99. ext4_discard_preallocations(inode);
  100. return ret;
  101. }
  102. /*
  103. * Called at the last iput() if i_nlink is zero.
  104. */
  105. void ext4_evict_inode(struct inode *inode)
  106. {
  107. handle_t *handle;
  108. int err;
  109. trace_ext4_evict_inode(inode);
  110. ext4_ioend_wait(inode);
  111. if (inode->i_nlink) {
  112. /*
  113. * When journalling data dirty buffers are tracked only in the
  114. * journal. So although mm thinks everything is clean and
  115. * ready for reaping the inode might still have some pages to
  116. * write in the running transaction or waiting to be
  117. * checkpointed. Thus calling jbd2_journal_invalidatepage()
  118. * (via truncate_inode_pages()) to discard these buffers can
  119. * cause data loss. Also even if we did not discard these
  120. * buffers, we would have no way to find them after the inode
  121. * is reaped and thus user could see stale data if he tries to
  122. * read them before the transaction is checkpointed. So be
  123. * careful and force everything to disk here... We use
  124. * ei->i_datasync_tid to store the newest transaction
  125. * containing inode's data.
  126. *
  127. * Note that directories do not have this problem because they
  128. * don't use page cache.
  129. */
  130. if (ext4_should_journal_data(inode) &&
  131. (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
  132. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  133. tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
  134. jbd2_log_start_commit(journal, commit_tid);
  135. jbd2_log_wait_commit(journal, commit_tid);
  136. filemap_write_and_wait(&inode->i_data);
  137. }
  138. truncate_inode_pages(&inode->i_data, 0);
  139. goto no_delete;
  140. }
  141. if (!is_bad_inode(inode))
  142. dquot_initialize(inode);
  143. if (ext4_should_order_data(inode))
  144. ext4_begin_ordered_truncate(inode, 0);
  145. truncate_inode_pages(&inode->i_data, 0);
  146. if (is_bad_inode(inode))
  147. goto no_delete;
  148. handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3);
  149. if (IS_ERR(handle)) {
  150. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  151. /*
  152. * If we're going to skip the normal cleanup, we still need to
  153. * make sure that the in-core orphan linked list is properly
  154. * cleaned up.
  155. */
  156. ext4_orphan_del(NULL, inode);
  157. goto no_delete;
  158. }
  159. if (IS_SYNC(inode))
  160. ext4_handle_sync(handle);
  161. inode->i_size = 0;
  162. err = ext4_mark_inode_dirty(handle, inode);
  163. if (err) {
  164. ext4_warning(inode->i_sb,
  165. "couldn't mark inode dirty (err %d)", err);
  166. goto stop_handle;
  167. }
  168. if (inode->i_blocks)
  169. ext4_truncate(inode);
  170. /*
  171. * ext4_ext_truncate() doesn't reserve any slop when it
  172. * restarts journal transactions; therefore there may not be
  173. * enough credits left in the handle to remove the inode from
  174. * the orphan list and set the dtime field.
  175. */
  176. if (!ext4_handle_has_enough_credits(handle, 3)) {
  177. err = ext4_journal_extend(handle, 3);
  178. if (err > 0)
  179. err = ext4_journal_restart(handle, 3);
  180. if (err != 0) {
  181. ext4_warning(inode->i_sb,
  182. "couldn't extend journal (err %d)", err);
  183. stop_handle:
  184. ext4_journal_stop(handle);
  185. ext4_orphan_del(NULL, inode);
  186. goto no_delete;
  187. }
  188. }
  189. /*
  190. * Kill off the orphan record which ext4_truncate created.
  191. * AKPM: I think this can be inside the above `if'.
  192. * Note that ext4_orphan_del() has to be able to cope with the
  193. * deletion of a non-existent orphan - this is because we don't
  194. * know if ext4_truncate() actually created an orphan record.
  195. * (Well, we could do this if we need to, but heck - it works)
  196. */
  197. ext4_orphan_del(handle, inode);
  198. EXT4_I(inode)->i_dtime = get_seconds();
  199. /*
  200. * One subtle ordering requirement: if anything has gone wrong
  201. * (transaction abort, IO errors, whatever), then we can still
  202. * do these next steps (the fs will already have been marked as
  203. * having errors), but we can't free the inode if the mark_dirty
  204. * fails.
  205. */
  206. if (ext4_mark_inode_dirty(handle, inode))
  207. /* If that failed, just do the required in-core inode clear. */
  208. ext4_clear_inode(inode);
  209. else
  210. ext4_free_inode(handle, inode);
  211. ext4_journal_stop(handle);
  212. return;
  213. no_delete:
  214. ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
  215. }
  216. #ifdef CONFIG_QUOTA
  217. qsize_t *ext4_get_reserved_space(struct inode *inode)
  218. {
  219. return &EXT4_I(inode)->i_reserved_quota;
  220. }
  221. #endif
  222. /*
  223. * Calculate the number of metadata blocks need to reserve
  224. * to allocate a block located at @lblock
  225. */
  226. static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  227. {
  228. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  229. return ext4_ext_calc_metadata_amount(inode, lblock);
  230. return ext4_ind_calc_metadata_amount(inode, lblock);
  231. }
  232. /*
  233. * Called with i_data_sem down, which is important since we can call
  234. * ext4_discard_preallocations() from here.
  235. */
  236. void ext4_da_update_reserve_space(struct inode *inode,
  237. int used, int quota_claim)
  238. {
  239. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  240. struct ext4_inode_info *ei = EXT4_I(inode);
  241. spin_lock(&ei->i_block_reservation_lock);
  242. trace_ext4_da_update_reserve_space(inode, used);
  243. if (unlikely(used > ei->i_reserved_data_blocks)) {
  244. ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
  245. "with only %d reserved data blocks\n",
  246. __func__, inode->i_ino, used,
  247. ei->i_reserved_data_blocks);
  248. WARN_ON(1);
  249. used = ei->i_reserved_data_blocks;
  250. }
  251. /* Update per-inode reservations */
  252. ei->i_reserved_data_blocks -= used;
  253. ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
  254. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  255. used + ei->i_allocated_meta_blocks);
  256. ei->i_allocated_meta_blocks = 0;
  257. if (ei->i_reserved_data_blocks == 0) {
  258. /*
  259. * We can release all of the reserved metadata blocks
  260. * only when we have written all of the delayed
  261. * allocation blocks.
  262. */
  263. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  264. ei->i_reserved_meta_blocks);
  265. ei->i_reserved_meta_blocks = 0;
  266. ei->i_da_metadata_calc_len = 0;
  267. }
  268. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  269. /* Update quota subsystem for data blocks */
  270. if (quota_claim)
  271. dquot_claim_block(inode, used);
  272. else {
  273. /*
  274. * We did fallocate with an offset that is already delayed
  275. * allocated. So on delayed allocated writeback we should
  276. * not re-claim the quota for fallocated blocks.
  277. */
  278. dquot_release_reservation_block(inode, used);
  279. }
  280. /*
  281. * If we have done all the pending block allocations and if
  282. * there aren't any writers on the inode, we can discard the
  283. * inode's preallocations.
  284. */
  285. if ((ei->i_reserved_data_blocks == 0) &&
  286. (atomic_read(&inode->i_writecount) == 0))
  287. ext4_discard_preallocations(inode);
  288. }
  289. static int __check_block_validity(struct inode *inode, const char *func,
  290. unsigned int line,
  291. struct ext4_map_blocks *map)
  292. {
  293. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
  294. map->m_len)) {
  295. ext4_error_inode(inode, func, line, map->m_pblk,
  296. "lblock %lu mapped to illegal pblock "
  297. "(length %d)", (unsigned long) map->m_lblk,
  298. map->m_len);
  299. return -EIO;
  300. }
  301. return 0;
  302. }
  303. #define check_block_validity(inode, map) \
  304. __check_block_validity((inode), __func__, __LINE__, (map))
  305. /*
  306. * Return the number of contiguous dirty pages in a given inode
  307. * starting at page frame idx.
  308. */
  309. static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
  310. unsigned int max_pages)
  311. {
  312. struct address_space *mapping = inode->i_mapping;
  313. pgoff_t index;
  314. struct pagevec pvec;
  315. pgoff_t num = 0;
  316. int i, nr_pages, done = 0;
  317. if (max_pages == 0)
  318. return 0;
  319. pagevec_init(&pvec, 0);
  320. while (!done) {
  321. index = idx;
  322. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  323. PAGECACHE_TAG_DIRTY,
  324. (pgoff_t)PAGEVEC_SIZE);
  325. if (nr_pages == 0)
  326. break;
  327. for (i = 0; i < nr_pages; i++) {
  328. struct page *page = pvec.pages[i];
  329. struct buffer_head *bh, *head;
  330. lock_page(page);
  331. if (unlikely(page->mapping != mapping) ||
  332. !PageDirty(page) ||
  333. PageWriteback(page) ||
  334. page->index != idx) {
  335. done = 1;
  336. unlock_page(page);
  337. break;
  338. }
  339. if (page_has_buffers(page)) {
  340. bh = head = page_buffers(page);
  341. do {
  342. if (!buffer_delay(bh) &&
  343. !buffer_unwritten(bh))
  344. done = 1;
  345. bh = bh->b_this_page;
  346. } while (!done && (bh != head));
  347. }
  348. unlock_page(page);
  349. if (done)
  350. break;
  351. idx++;
  352. num++;
  353. if (num >= max_pages) {
  354. done = 1;
  355. break;
  356. }
  357. }
  358. pagevec_release(&pvec);
  359. }
  360. return num;
  361. }
  362. /*
  363. * The ext4_map_blocks() function tries to look up the requested blocks,
  364. * and returns if the blocks are already mapped.
  365. *
  366. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  367. * and store the allocated blocks in the result buffer head and mark it
  368. * mapped.
  369. *
  370. * If file type is extents based, it will call ext4_ext_map_blocks(),
  371. * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  372. * based files
  373. *
  374. * On success, it returns the number of blocks being mapped or allocate.
  375. * if create==0 and the blocks are pre-allocated and uninitialized block,
  376. * the result buffer head is unmapped. If the create ==1, it will make sure
  377. * the buffer head is mapped.
  378. *
  379. * It returns 0 if plain look up failed (blocks have not been allocated), in
  380. * that casem, buffer head is unmapped
  381. *
  382. * It returns the error in case of allocation failure.
  383. */
  384. int ext4_map_blocks(handle_t *handle, struct inode *inode,
  385. struct ext4_map_blocks *map, int flags)
  386. {
  387. int retval;
  388. map->m_flags = 0;
  389. ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
  390. "logical block %lu\n", inode->i_ino, flags, map->m_len,
  391. (unsigned long) map->m_lblk);
  392. /*
  393. * Try to see if we can get the block without requesting a new
  394. * file system block.
  395. */
  396. down_read((&EXT4_I(inode)->i_data_sem));
  397. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  398. retval = ext4_ext_map_blocks(handle, inode, map, 0);
  399. } else {
  400. retval = ext4_ind_map_blocks(handle, inode, map, 0);
  401. }
  402. up_read((&EXT4_I(inode)->i_data_sem));
  403. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  404. int ret = check_block_validity(inode, map);
  405. if (ret != 0)
  406. return ret;
  407. }
  408. /* If it is only a block(s) look up */
  409. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  410. return retval;
  411. /*
  412. * Returns if the blocks have already allocated
  413. *
  414. * Note that if blocks have been preallocated
  415. * ext4_ext_get_block() returns th create = 0
  416. * with buffer head unmapped.
  417. */
  418. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
  419. return retval;
  420. /*
  421. * When we call get_blocks without the create flag, the
  422. * BH_Unwritten flag could have gotten set if the blocks
  423. * requested were part of a uninitialized extent. We need to
  424. * clear this flag now that we are committed to convert all or
  425. * part of the uninitialized extent to be an initialized
  426. * extent. This is because we need to avoid the combination
  427. * of BH_Unwritten and BH_Mapped flags being simultaneously
  428. * set on the buffer_head.
  429. */
  430. map->m_flags &= ~EXT4_MAP_UNWRITTEN;
  431. /*
  432. * New blocks allocate and/or writing to uninitialized extent
  433. * will possibly result in updating i_data, so we take
  434. * the write lock of i_data_sem, and call get_blocks()
  435. * with create == 1 flag.
  436. */
  437. down_write((&EXT4_I(inode)->i_data_sem));
  438. /*
  439. * if the caller is from delayed allocation writeout path
  440. * we have already reserved fs blocks for allocation
  441. * let the underlying get_block() function know to
  442. * avoid double accounting
  443. */
  444. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  445. ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  446. /*
  447. * We need to check for EXT4 here because migrate
  448. * could have changed the inode type in between
  449. */
  450. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  451. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  452. } else {
  453. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  454. if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
  455. /*
  456. * We allocated new blocks which will result in
  457. * i_data's format changing. Force the migrate
  458. * to fail by clearing migrate flags
  459. */
  460. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  461. }
  462. /*
  463. * Update reserved blocks/metadata blocks after successful
  464. * block allocation which had been deferred till now. We don't
  465. * support fallocate for non extent files. So we can update
  466. * reserve space here.
  467. */
  468. if ((retval > 0) &&
  469. (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
  470. ext4_da_update_reserve_space(inode, retval, 1);
  471. }
  472. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  473. ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  474. up_write((&EXT4_I(inode)->i_data_sem));
  475. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  476. int ret = check_block_validity(inode, map);
  477. if (ret != 0)
  478. return ret;
  479. }
  480. return retval;
  481. }
  482. /* Maximum number of blocks we map for direct IO at once. */
  483. #define DIO_MAX_BLOCKS 4096
  484. static int _ext4_get_block(struct inode *inode, sector_t iblock,
  485. struct buffer_head *bh, int flags)
  486. {
  487. handle_t *handle = ext4_journal_current_handle();
  488. struct ext4_map_blocks map;
  489. int ret = 0, started = 0;
  490. int dio_credits;
  491. map.m_lblk = iblock;
  492. map.m_len = bh->b_size >> inode->i_blkbits;
  493. if (flags && !handle) {
  494. /* Direct IO write... */
  495. if (map.m_len > DIO_MAX_BLOCKS)
  496. map.m_len = DIO_MAX_BLOCKS;
  497. dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
  498. handle = ext4_journal_start(inode, dio_credits);
  499. if (IS_ERR(handle)) {
  500. ret = PTR_ERR(handle);
  501. return ret;
  502. }
  503. started = 1;
  504. }
  505. ret = ext4_map_blocks(handle, inode, &map, flags);
  506. if (ret > 0) {
  507. map_bh(bh, inode->i_sb, map.m_pblk);
  508. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  509. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  510. ret = 0;
  511. }
  512. if (started)
  513. ext4_journal_stop(handle);
  514. return ret;
  515. }
  516. int ext4_get_block(struct inode *inode, sector_t iblock,
  517. struct buffer_head *bh, int create)
  518. {
  519. return _ext4_get_block(inode, iblock, bh,
  520. create ? EXT4_GET_BLOCKS_CREATE : 0);
  521. }
  522. /*
  523. * `handle' can be NULL if create is zero
  524. */
  525. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  526. ext4_lblk_t block, int create, int *errp)
  527. {
  528. struct ext4_map_blocks map;
  529. struct buffer_head *bh;
  530. int fatal = 0, err;
  531. J_ASSERT(handle != NULL || create == 0);
  532. map.m_lblk = block;
  533. map.m_len = 1;
  534. err = ext4_map_blocks(handle, inode, &map,
  535. create ? EXT4_GET_BLOCKS_CREATE : 0);
  536. if (err < 0)
  537. *errp = err;
  538. if (err <= 0)
  539. return NULL;
  540. *errp = 0;
  541. bh = sb_getblk(inode->i_sb, map.m_pblk);
  542. if (!bh) {
  543. *errp = -EIO;
  544. return NULL;
  545. }
  546. if (map.m_flags & EXT4_MAP_NEW) {
  547. J_ASSERT(create != 0);
  548. J_ASSERT(handle != NULL);
  549. /*
  550. * Now that we do not always journal data, we should
  551. * keep in mind whether this should always journal the
  552. * new buffer as metadata. For now, regular file
  553. * writes use ext4_get_block instead, so it's not a
  554. * problem.
  555. */
  556. lock_buffer(bh);
  557. BUFFER_TRACE(bh, "call get_create_access");
  558. fatal = ext4_journal_get_create_access(handle, bh);
  559. if (!fatal && !buffer_uptodate(bh)) {
  560. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  561. set_buffer_uptodate(bh);
  562. }
  563. unlock_buffer(bh);
  564. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  565. err = ext4_handle_dirty_metadata(handle, inode, bh);
  566. if (!fatal)
  567. fatal = err;
  568. } else {
  569. BUFFER_TRACE(bh, "not a new buffer");
  570. }
  571. if (fatal) {
  572. *errp = fatal;
  573. brelse(bh);
  574. bh = NULL;
  575. }
  576. return bh;
  577. }
  578. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  579. ext4_lblk_t block, int create, int *err)
  580. {
  581. struct buffer_head *bh;
  582. bh = ext4_getblk(handle, inode, block, create, err);
  583. if (!bh)
  584. return bh;
  585. if (buffer_uptodate(bh))
  586. return bh;
  587. ll_rw_block(READ_META, 1, &bh);
  588. wait_on_buffer(bh);
  589. if (buffer_uptodate(bh))
  590. return bh;
  591. put_bh(bh);
  592. *err = -EIO;
  593. return NULL;
  594. }
  595. static int walk_page_buffers(handle_t *handle,
  596. struct buffer_head *head,
  597. unsigned from,
  598. unsigned to,
  599. int *partial,
  600. int (*fn)(handle_t *handle,
  601. struct buffer_head *bh))
  602. {
  603. struct buffer_head *bh;
  604. unsigned block_start, block_end;
  605. unsigned blocksize = head->b_size;
  606. int err, ret = 0;
  607. struct buffer_head *next;
  608. for (bh = head, block_start = 0;
  609. ret == 0 && (bh != head || !block_start);
  610. block_start = block_end, bh = next) {
  611. next = bh->b_this_page;
  612. block_end = block_start + blocksize;
  613. if (block_end <= from || block_start >= to) {
  614. if (partial && !buffer_uptodate(bh))
  615. *partial = 1;
  616. continue;
  617. }
  618. err = (*fn)(handle, bh);
  619. if (!ret)
  620. ret = err;
  621. }
  622. return ret;
  623. }
  624. /*
  625. * To preserve ordering, it is essential that the hole instantiation and
  626. * the data write be encapsulated in a single transaction. We cannot
  627. * close off a transaction and start a new one between the ext4_get_block()
  628. * and the commit_write(). So doing the jbd2_journal_start at the start of
  629. * prepare_write() is the right place.
  630. *
  631. * Also, this function can nest inside ext4_writepage() ->
  632. * block_write_full_page(). In that case, we *know* that ext4_writepage()
  633. * has generated enough buffer credits to do the whole page. So we won't
  634. * block on the journal in that case, which is good, because the caller may
  635. * be PF_MEMALLOC.
  636. *
  637. * By accident, ext4 can be reentered when a transaction is open via
  638. * quota file writes. If we were to commit the transaction while thus
  639. * reentered, there can be a deadlock - we would be holding a quota
  640. * lock, and the commit would never complete if another thread had a
  641. * transaction open and was blocking on the quota lock - a ranking
  642. * violation.
  643. *
  644. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  645. * will _not_ run commit under these circumstances because handle->h_ref
  646. * is elevated. We'll still have enough credits for the tiny quotafile
  647. * write.
  648. */
  649. static int do_journal_get_write_access(handle_t *handle,
  650. struct buffer_head *bh)
  651. {
  652. int dirty = buffer_dirty(bh);
  653. int ret;
  654. if (!buffer_mapped(bh) || buffer_freed(bh))
  655. return 0;
  656. /*
  657. * __block_write_begin() could have dirtied some buffers. Clean
  658. * the dirty bit as jbd2_journal_get_write_access() could complain
  659. * otherwise about fs integrity issues. Setting of the dirty bit
  660. * by __block_write_begin() isn't a real problem here as we clear
  661. * the bit before releasing a page lock and thus writeback cannot
  662. * ever write the buffer.
  663. */
  664. if (dirty)
  665. clear_buffer_dirty(bh);
  666. ret = ext4_journal_get_write_access(handle, bh);
  667. if (!ret && dirty)
  668. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  669. return ret;
  670. }
  671. static int ext4_get_block_write(struct inode *inode, sector_t iblock,
  672. struct buffer_head *bh_result, int create);
  673. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  674. loff_t pos, unsigned len, unsigned flags,
  675. struct page **pagep, void **fsdata)
  676. {
  677. struct inode *inode = mapping->host;
  678. int ret, needed_blocks;
  679. handle_t *handle;
  680. int retries = 0;
  681. struct page *page;
  682. pgoff_t index;
  683. unsigned from, to;
  684. trace_ext4_write_begin(inode, pos, len, flags);
  685. /*
  686. * Reserve one block more for addition to orphan list in case
  687. * we allocate blocks but write fails for some reason
  688. */
  689. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  690. index = pos >> PAGE_CACHE_SHIFT;
  691. from = pos & (PAGE_CACHE_SIZE - 1);
  692. to = from + len;
  693. retry:
  694. handle = ext4_journal_start(inode, needed_blocks);
  695. if (IS_ERR(handle)) {
  696. ret = PTR_ERR(handle);
  697. goto out;
  698. }
  699. /* We cannot recurse into the filesystem as the transaction is already
  700. * started */
  701. flags |= AOP_FLAG_NOFS;
  702. page = grab_cache_page_write_begin(mapping, index, flags);
  703. if (!page) {
  704. ext4_journal_stop(handle);
  705. ret = -ENOMEM;
  706. goto out;
  707. }
  708. *pagep = page;
  709. if (ext4_should_dioread_nolock(inode))
  710. ret = __block_write_begin(page, pos, len, ext4_get_block_write);
  711. else
  712. ret = __block_write_begin(page, pos, len, ext4_get_block);
  713. if (!ret && ext4_should_journal_data(inode)) {
  714. ret = walk_page_buffers(handle, page_buffers(page),
  715. from, to, NULL, do_journal_get_write_access);
  716. }
  717. if (ret) {
  718. unlock_page(page);
  719. page_cache_release(page);
  720. /*
  721. * __block_write_begin may have instantiated a few blocks
  722. * outside i_size. Trim these off again. Don't need
  723. * i_size_read because we hold i_mutex.
  724. *
  725. * Add inode to orphan list in case we crash before
  726. * truncate finishes
  727. */
  728. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  729. ext4_orphan_add(handle, inode);
  730. ext4_journal_stop(handle);
  731. if (pos + len > inode->i_size) {
  732. ext4_truncate_failed_write(inode);
  733. /*
  734. * If truncate failed early the inode might
  735. * still be on the orphan list; we need to
  736. * make sure the inode is removed from the
  737. * orphan list in that case.
  738. */
  739. if (inode->i_nlink)
  740. ext4_orphan_del(NULL, inode);
  741. }
  742. }
  743. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  744. goto retry;
  745. out:
  746. return ret;
  747. }
  748. /* For write_end() in data=journal mode */
  749. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  750. {
  751. if (!buffer_mapped(bh) || buffer_freed(bh))
  752. return 0;
  753. set_buffer_uptodate(bh);
  754. return ext4_handle_dirty_metadata(handle, NULL, bh);
  755. }
  756. static int ext4_generic_write_end(struct file *file,
  757. struct address_space *mapping,
  758. loff_t pos, unsigned len, unsigned copied,
  759. struct page *page, void *fsdata)
  760. {
  761. int i_size_changed = 0;
  762. struct inode *inode = mapping->host;
  763. handle_t *handle = ext4_journal_current_handle();
  764. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  765. /*
  766. * No need to use i_size_read() here, the i_size
  767. * cannot change under us because we hold i_mutex.
  768. *
  769. * But it's important to update i_size while still holding page lock:
  770. * page writeout could otherwise come in and zero beyond i_size.
  771. */
  772. if (pos + copied > inode->i_size) {
  773. i_size_write(inode, pos + copied);
  774. i_size_changed = 1;
  775. }
  776. if (pos + copied > EXT4_I(inode)->i_disksize) {
  777. /* We need to mark inode dirty even if
  778. * new_i_size is less that inode->i_size
  779. * bu greater than i_disksize.(hint delalloc)
  780. */
  781. ext4_update_i_disksize(inode, (pos + copied));
  782. i_size_changed = 1;
  783. }
  784. unlock_page(page);
  785. page_cache_release(page);
  786. /*
  787. * Don't mark the inode dirty under page lock. First, it unnecessarily
  788. * makes the holding time of page lock longer. Second, it forces lock
  789. * ordering of page lock and transaction start for journaling
  790. * filesystems.
  791. */
  792. if (i_size_changed)
  793. ext4_mark_inode_dirty(handle, inode);
  794. return copied;
  795. }
  796. /*
  797. * We need to pick up the new inode size which generic_commit_write gave us
  798. * `file' can be NULL - eg, when called from page_symlink().
  799. *
  800. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  801. * buffers are managed internally.
  802. */
  803. static int ext4_ordered_write_end(struct file *file,
  804. struct address_space *mapping,
  805. loff_t pos, unsigned len, unsigned copied,
  806. struct page *page, void *fsdata)
  807. {
  808. handle_t *handle = ext4_journal_current_handle();
  809. struct inode *inode = mapping->host;
  810. int ret = 0, ret2;
  811. trace_ext4_ordered_write_end(inode, pos, len, copied);
  812. ret = ext4_jbd2_file_inode(handle, inode);
  813. if (ret == 0) {
  814. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  815. page, fsdata);
  816. copied = ret2;
  817. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  818. /* if we have allocated more blocks and copied
  819. * less. We will have blocks allocated outside
  820. * inode->i_size. So truncate them
  821. */
  822. ext4_orphan_add(handle, inode);
  823. if (ret2 < 0)
  824. ret = ret2;
  825. }
  826. ret2 = ext4_journal_stop(handle);
  827. if (!ret)
  828. ret = ret2;
  829. if (pos + len > inode->i_size) {
  830. ext4_truncate_failed_write(inode);
  831. /*
  832. * If truncate failed early the inode might still be
  833. * on the orphan list; we need to make sure the inode
  834. * is removed from the orphan list in that case.
  835. */
  836. if (inode->i_nlink)
  837. ext4_orphan_del(NULL, inode);
  838. }
  839. return ret ? ret : copied;
  840. }
  841. static int ext4_writeback_write_end(struct file *file,
  842. struct address_space *mapping,
  843. loff_t pos, unsigned len, unsigned copied,
  844. struct page *page, void *fsdata)
  845. {
  846. handle_t *handle = ext4_journal_current_handle();
  847. struct inode *inode = mapping->host;
  848. int ret = 0, ret2;
  849. trace_ext4_writeback_write_end(inode, pos, len, copied);
  850. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  851. page, fsdata);
  852. copied = ret2;
  853. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  854. /* if we have allocated more blocks and copied
  855. * less. We will have blocks allocated outside
  856. * inode->i_size. So truncate them
  857. */
  858. ext4_orphan_add(handle, inode);
  859. if (ret2 < 0)
  860. ret = ret2;
  861. ret2 = ext4_journal_stop(handle);
  862. if (!ret)
  863. ret = ret2;
  864. if (pos + len > inode->i_size) {
  865. ext4_truncate_failed_write(inode);
  866. /*
  867. * If truncate failed early the inode might still be
  868. * on the orphan list; we need to make sure the inode
  869. * is removed from the orphan list in that case.
  870. */
  871. if (inode->i_nlink)
  872. ext4_orphan_del(NULL, inode);
  873. }
  874. return ret ? ret : copied;
  875. }
  876. static int ext4_journalled_write_end(struct file *file,
  877. struct address_space *mapping,
  878. loff_t pos, unsigned len, unsigned copied,
  879. struct page *page, void *fsdata)
  880. {
  881. handle_t *handle = ext4_journal_current_handle();
  882. struct inode *inode = mapping->host;
  883. int ret = 0, ret2;
  884. int partial = 0;
  885. unsigned from, to;
  886. loff_t new_i_size;
  887. trace_ext4_journalled_write_end(inode, pos, len, copied);
  888. from = pos & (PAGE_CACHE_SIZE - 1);
  889. to = from + len;
  890. BUG_ON(!ext4_handle_valid(handle));
  891. if (copied < len) {
  892. if (!PageUptodate(page))
  893. copied = 0;
  894. page_zero_new_buffers(page, from+copied, to);
  895. }
  896. ret = walk_page_buffers(handle, page_buffers(page), from,
  897. to, &partial, write_end_fn);
  898. if (!partial)
  899. SetPageUptodate(page);
  900. new_i_size = pos + copied;
  901. if (new_i_size > inode->i_size)
  902. i_size_write(inode, pos+copied);
  903. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  904. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  905. if (new_i_size > EXT4_I(inode)->i_disksize) {
  906. ext4_update_i_disksize(inode, new_i_size);
  907. ret2 = ext4_mark_inode_dirty(handle, inode);
  908. if (!ret)
  909. ret = ret2;
  910. }
  911. unlock_page(page);
  912. page_cache_release(page);
  913. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  914. /* if we have allocated more blocks and copied
  915. * less. We will have blocks allocated outside
  916. * inode->i_size. So truncate them
  917. */
  918. ext4_orphan_add(handle, inode);
  919. ret2 = ext4_journal_stop(handle);
  920. if (!ret)
  921. ret = ret2;
  922. if (pos + len > inode->i_size) {
  923. ext4_truncate_failed_write(inode);
  924. /*
  925. * If truncate failed early the inode might still be
  926. * on the orphan list; we need to make sure the inode
  927. * is removed from the orphan list in that case.
  928. */
  929. if (inode->i_nlink)
  930. ext4_orphan_del(NULL, inode);
  931. }
  932. return ret ? ret : copied;
  933. }
  934. /*
  935. * Reserve a single block located at lblock
  936. */
  937. static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
  938. {
  939. int retries = 0;
  940. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  941. struct ext4_inode_info *ei = EXT4_I(inode);
  942. unsigned long md_needed;
  943. int ret;
  944. /*
  945. * recalculate the amount of metadata blocks to reserve
  946. * in order to allocate nrblocks
  947. * worse case is one extent per block
  948. */
  949. repeat:
  950. spin_lock(&ei->i_block_reservation_lock);
  951. md_needed = ext4_calc_metadata_amount(inode, lblock);
  952. trace_ext4_da_reserve_space(inode, md_needed);
  953. spin_unlock(&ei->i_block_reservation_lock);
  954. /*
  955. * We will charge metadata quota at writeout time; this saves
  956. * us from metadata over-estimation, though we may go over by
  957. * a small amount in the end. Here we just reserve for data.
  958. */
  959. ret = dquot_reserve_block(inode, 1);
  960. if (ret)
  961. return ret;
  962. /*
  963. * We do still charge estimated metadata to the sb though;
  964. * we cannot afford to run out of free blocks.
  965. */
  966. if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) {
  967. dquot_release_reservation_block(inode, 1);
  968. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  969. yield();
  970. goto repeat;
  971. }
  972. return -ENOSPC;
  973. }
  974. spin_lock(&ei->i_block_reservation_lock);
  975. ei->i_reserved_data_blocks++;
  976. ei->i_reserved_meta_blocks += md_needed;
  977. spin_unlock(&ei->i_block_reservation_lock);
  978. return 0; /* success */
  979. }
  980. static void ext4_da_release_space(struct inode *inode, int to_free)
  981. {
  982. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  983. struct ext4_inode_info *ei = EXT4_I(inode);
  984. if (!to_free)
  985. return; /* Nothing to release, exit */
  986. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  987. trace_ext4_da_release_space(inode, to_free);
  988. if (unlikely(to_free > ei->i_reserved_data_blocks)) {
  989. /*
  990. * if there aren't enough reserved blocks, then the
  991. * counter is messed up somewhere. Since this
  992. * function is called from invalidate page, it's
  993. * harmless to return without any action.
  994. */
  995. ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
  996. "ino %lu, to_free %d with only %d reserved "
  997. "data blocks\n", inode->i_ino, to_free,
  998. ei->i_reserved_data_blocks);
  999. WARN_ON(1);
  1000. to_free = ei->i_reserved_data_blocks;
  1001. }
  1002. ei->i_reserved_data_blocks -= to_free;
  1003. if (ei->i_reserved_data_blocks == 0) {
  1004. /*
  1005. * We can release all of the reserved metadata blocks
  1006. * only when we have written all of the delayed
  1007. * allocation blocks.
  1008. */
  1009. percpu_counter_sub(&sbi->s_dirtyblocks_counter,
  1010. ei->i_reserved_meta_blocks);
  1011. ei->i_reserved_meta_blocks = 0;
  1012. ei->i_da_metadata_calc_len = 0;
  1013. }
  1014. /* update fs dirty data blocks counter */
  1015. percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
  1016. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1017. dquot_release_reservation_block(inode, to_free);
  1018. }
  1019. static void ext4_da_page_release_reservation(struct page *page,
  1020. unsigned long offset)
  1021. {
  1022. int to_release = 0;
  1023. struct buffer_head *head, *bh;
  1024. unsigned int curr_off = 0;
  1025. head = page_buffers(page);
  1026. bh = head;
  1027. do {
  1028. unsigned int next_off = curr_off + bh->b_size;
  1029. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1030. to_release++;
  1031. clear_buffer_delay(bh);
  1032. }
  1033. curr_off = next_off;
  1034. } while ((bh = bh->b_this_page) != head);
  1035. ext4_da_release_space(page->mapping->host, to_release);
  1036. }
  1037. /*
  1038. * Delayed allocation stuff
  1039. */
  1040. /*
  1041. * mpage_da_submit_io - walks through extent of pages and try to write
  1042. * them with writepage() call back
  1043. *
  1044. * @mpd->inode: inode
  1045. * @mpd->first_page: first page of the extent
  1046. * @mpd->next_page: page after the last page of the extent
  1047. *
  1048. * By the time mpage_da_submit_io() is called we expect all blocks
  1049. * to be allocated. this may be wrong if allocation failed.
  1050. *
  1051. * As pages are already locked by write_cache_pages(), we can't use it
  1052. */
  1053. static int mpage_da_submit_io(struct mpage_da_data *mpd,
  1054. struct ext4_map_blocks *map)
  1055. {
  1056. struct pagevec pvec;
  1057. unsigned long index, end;
  1058. int ret = 0, err, nr_pages, i;
  1059. struct inode *inode = mpd->inode;
  1060. struct address_space *mapping = inode->i_mapping;
  1061. loff_t size = i_size_read(inode);
  1062. unsigned int len, block_start;
  1063. struct buffer_head *bh, *page_bufs = NULL;
  1064. int journal_data = ext4_should_journal_data(inode);
  1065. sector_t pblock = 0, cur_logical = 0;
  1066. struct ext4_io_submit io_submit;
  1067. BUG_ON(mpd->next_page <= mpd->first_page);
  1068. memset(&io_submit, 0, sizeof(io_submit));
  1069. /*
  1070. * We need to start from the first_page to the next_page - 1
  1071. * to make sure we also write the mapped dirty buffer_heads.
  1072. * If we look at mpd->b_blocknr we would only be looking
  1073. * at the currently mapped buffer_heads.
  1074. */
  1075. index = mpd->first_page;
  1076. end = mpd->next_page - 1;
  1077. pagevec_init(&pvec, 0);
  1078. while (index <= end) {
  1079. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1080. if (nr_pages == 0)
  1081. break;
  1082. for (i = 0; i < nr_pages; i++) {
  1083. int commit_write = 0, skip_page = 0;
  1084. struct page *page = pvec.pages[i];
  1085. index = page->index;
  1086. if (index > end)
  1087. break;
  1088. if (index == size >> PAGE_CACHE_SHIFT)
  1089. len = size & ~PAGE_CACHE_MASK;
  1090. else
  1091. len = PAGE_CACHE_SIZE;
  1092. if (map) {
  1093. cur_logical = index << (PAGE_CACHE_SHIFT -
  1094. inode->i_blkbits);
  1095. pblock = map->m_pblk + (cur_logical -
  1096. map->m_lblk);
  1097. }
  1098. index++;
  1099. BUG_ON(!PageLocked(page));
  1100. BUG_ON(PageWriteback(page));
  1101. /*
  1102. * If the page does not have buffers (for
  1103. * whatever reason), try to create them using
  1104. * __block_write_begin. If this fails,
  1105. * skip the page and move on.
  1106. */
  1107. if (!page_has_buffers(page)) {
  1108. if (__block_write_begin(page, 0, len,
  1109. noalloc_get_block_write)) {
  1110. skip_page:
  1111. unlock_page(page);
  1112. continue;
  1113. }
  1114. commit_write = 1;
  1115. }
  1116. bh = page_bufs = page_buffers(page);
  1117. block_start = 0;
  1118. do {
  1119. if (!bh)
  1120. goto skip_page;
  1121. if (map && (cur_logical >= map->m_lblk) &&
  1122. (cur_logical <= (map->m_lblk +
  1123. (map->m_len - 1)))) {
  1124. if (buffer_delay(bh)) {
  1125. clear_buffer_delay(bh);
  1126. bh->b_blocknr = pblock;
  1127. }
  1128. if (buffer_unwritten(bh) ||
  1129. buffer_mapped(bh))
  1130. BUG_ON(bh->b_blocknr != pblock);
  1131. if (map->m_flags & EXT4_MAP_UNINIT)
  1132. set_buffer_uninit(bh);
  1133. clear_buffer_unwritten(bh);
  1134. }
  1135. /* skip page if block allocation undone */
  1136. if (buffer_delay(bh) || buffer_unwritten(bh))
  1137. skip_page = 1;
  1138. bh = bh->b_this_page;
  1139. block_start += bh->b_size;
  1140. cur_logical++;
  1141. pblock++;
  1142. } while (bh != page_bufs);
  1143. if (skip_page)
  1144. goto skip_page;
  1145. if (commit_write)
  1146. /* mark the buffer_heads as dirty & uptodate */
  1147. block_commit_write(page, 0, len);
  1148. clear_page_dirty_for_io(page);
  1149. /*
  1150. * Delalloc doesn't support data journalling,
  1151. * but eventually maybe we'll lift this
  1152. * restriction.
  1153. */
  1154. if (unlikely(journal_data && PageChecked(page)))
  1155. err = __ext4_journalled_writepage(page, len);
  1156. else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT))
  1157. err = ext4_bio_write_page(&io_submit, page,
  1158. len, mpd->wbc);
  1159. else if (buffer_uninit(page_bufs)) {
  1160. ext4_set_bh_endio(page_bufs, inode);
  1161. err = block_write_full_page_endio(page,
  1162. noalloc_get_block_write,
  1163. mpd->wbc, ext4_end_io_buffer_write);
  1164. } else
  1165. err = block_write_full_page(page,
  1166. noalloc_get_block_write, mpd->wbc);
  1167. if (!err)
  1168. mpd->pages_written++;
  1169. /*
  1170. * In error case, we have to continue because
  1171. * remaining pages are still locked
  1172. */
  1173. if (ret == 0)
  1174. ret = err;
  1175. }
  1176. pagevec_release(&pvec);
  1177. }
  1178. ext4_io_submit(&io_submit);
  1179. return ret;
  1180. }
  1181. static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
  1182. {
  1183. int nr_pages, i;
  1184. pgoff_t index, end;
  1185. struct pagevec pvec;
  1186. struct inode *inode = mpd->inode;
  1187. struct address_space *mapping = inode->i_mapping;
  1188. index = mpd->first_page;
  1189. end = mpd->next_page - 1;
  1190. while (index <= end) {
  1191. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1192. if (nr_pages == 0)
  1193. break;
  1194. for (i = 0; i < nr_pages; i++) {
  1195. struct page *page = pvec.pages[i];
  1196. if (page->index > end)
  1197. break;
  1198. BUG_ON(!PageLocked(page));
  1199. BUG_ON(PageWriteback(page));
  1200. block_invalidatepage(page, 0);
  1201. ClearPageUptodate(page);
  1202. unlock_page(page);
  1203. }
  1204. index = pvec.pages[nr_pages - 1]->index + 1;
  1205. pagevec_release(&pvec);
  1206. }
  1207. return;
  1208. }
  1209. static void ext4_print_free_blocks(struct inode *inode)
  1210. {
  1211. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1212. printk(KERN_CRIT "Total free blocks count %lld\n",
  1213. ext4_count_free_blocks(inode->i_sb));
  1214. printk(KERN_CRIT "Free/Dirty block details\n");
  1215. printk(KERN_CRIT "free_blocks=%lld\n",
  1216. (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
  1217. printk(KERN_CRIT "dirty_blocks=%lld\n",
  1218. (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  1219. printk(KERN_CRIT "Block reservation details\n");
  1220. printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
  1221. EXT4_I(inode)->i_reserved_data_blocks);
  1222. printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
  1223. EXT4_I(inode)->i_reserved_meta_blocks);
  1224. return;
  1225. }
  1226. /*
  1227. * mpage_da_map_and_submit - go through given space, map them
  1228. * if necessary, and then submit them for I/O
  1229. *
  1230. * @mpd - bh describing space
  1231. *
  1232. * The function skips space we know is already mapped to disk blocks.
  1233. *
  1234. */
  1235. static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
  1236. {
  1237. int err, blks, get_blocks_flags;
  1238. struct ext4_map_blocks map, *mapp = NULL;
  1239. sector_t next = mpd->b_blocknr;
  1240. unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
  1241. loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
  1242. handle_t *handle = NULL;
  1243. /*
  1244. * If the blocks are mapped already, or we couldn't accumulate
  1245. * any blocks, then proceed immediately to the submission stage.
  1246. */
  1247. if ((mpd->b_size == 0) ||
  1248. ((mpd->b_state & (1 << BH_Mapped)) &&
  1249. !(mpd->b_state & (1 << BH_Delay)) &&
  1250. !(mpd->b_state & (1 << BH_Unwritten))))
  1251. goto submit_io;
  1252. handle = ext4_journal_current_handle();
  1253. BUG_ON(!handle);
  1254. /*
  1255. * Call ext4_map_blocks() to allocate any delayed allocation
  1256. * blocks, or to convert an uninitialized extent to be
  1257. * initialized (in the case where we have written into
  1258. * one or more preallocated blocks).
  1259. *
  1260. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
  1261. * indicate that we are on the delayed allocation path. This
  1262. * affects functions in many different parts of the allocation
  1263. * call path. This flag exists primarily because we don't
  1264. * want to change *many* call functions, so ext4_map_blocks()
  1265. * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
  1266. * inode's allocation semaphore is taken.
  1267. *
  1268. * If the blocks in questions were delalloc blocks, set
  1269. * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
  1270. * variables are updated after the blocks have been allocated.
  1271. */
  1272. map.m_lblk = next;
  1273. map.m_len = max_blocks;
  1274. get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
  1275. if (ext4_should_dioread_nolock(mpd->inode))
  1276. get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
  1277. if (mpd->b_state & (1 << BH_Delay))
  1278. get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  1279. blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
  1280. if (blks < 0) {
  1281. struct super_block *sb = mpd->inode->i_sb;
  1282. err = blks;
  1283. /*
  1284. * If get block returns EAGAIN or ENOSPC and there
  1285. * appears to be free blocks we will just let
  1286. * mpage_da_submit_io() unlock all of the pages.
  1287. */
  1288. if (err == -EAGAIN)
  1289. goto submit_io;
  1290. if (err == -ENOSPC &&
  1291. ext4_count_free_blocks(sb)) {
  1292. mpd->retval = err;
  1293. goto submit_io;
  1294. }
  1295. /*
  1296. * get block failure will cause us to loop in
  1297. * writepages, because a_ops->writepage won't be able
  1298. * to make progress. The page will be redirtied by
  1299. * writepage and writepages will again try to write
  1300. * the same.
  1301. */
  1302. if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
  1303. ext4_msg(sb, KERN_CRIT,
  1304. "delayed block allocation failed for inode %lu "
  1305. "at logical offset %llu with max blocks %zd "
  1306. "with error %d", mpd->inode->i_ino,
  1307. (unsigned long long) next,
  1308. mpd->b_size >> mpd->inode->i_blkbits, err);
  1309. ext4_msg(sb, KERN_CRIT,
  1310. "This should not happen!! Data will be lost\n");
  1311. if (err == -ENOSPC)
  1312. ext4_print_free_blocks(mpd->inode);
  1313. }
  1314. /* invalidate all the pages */
  1315. ext4_da_block_invalidatepages(mpd);
  1316. /* Mark this page range as having been completed */
  1317. mpd->io_done = 1;
  1318. return;
  1319. }
  1320. BUG_ON(blks == 0);
  1321. mapp = &map;
  1322. if (map.m_flags & EXT4_MAP_NEW) {
  1323. struct block_device *bdev = mpd->inode->i_sb->s_bdev;
  1324. int i;
  1325. for (i = 0; i < map.m_len; i++)
  1326. unmap_underlying_metadata(bdev, map.m_pblk + i);
  1327. }
  1328. if (ext4_should_order_data(mpd->inode)) {
  1329. err = ext4_jbd2_file_inode(handle, mpd->inode);
  1330. if (err)
  1331. /* This only happens if the journal is aborted */
  1332. return;
  1333. }
  1334. /*
  1335. * Update on-disk size along with block allocation.
  1336. */
  1337. disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
  1338. if (disksize > i_size_read(mpd->inode))
  1339. disksize = i_size_read(mpd->inode);
  1340. if (disksize > EXT4_I(mpd->inode)->i_disksize) {
  1341. ext4_update_i_disksize(mpd->inode, disksize);
  1342. err = ext4_mark_inode_dirty(handle, mpd->inode);
  1343. if (err)
  1344. ext4_error(mpd->inode->i_sb,
  1345. "Failed to mark inode %lu dirty",
  1346. mpd->inode->i_ino);
  1347. }
  1348. submit_io:
  1349. mpage_da_submit_io(mpd, mapp);
  1350. mpd->io_done = 1;
  1351. }
  1352. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  1353. (1 << BH_Delay) | (1 << BH_Unwritten))
  1354. /*
  1355. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  1356. *
  1357. * @mpd->lbh - extent of blocks
  1358. * @logical - logical number of the block in the file
  1359. * @bh - bh of the block (used to access block's state)
  1360. *
  1361. * the function is used to collect contig. blocks in same state
  1362. */
  1363. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
  1364. sector_t logical, size_t b_size,
  1365. unsigned long b_state)
  1366. {
  1367. sector_t next;
  1368. int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
  1369. /*
  1370. * XXX Don't go larger than mballoc is willing to allocate
  1371. * This is a stopgap solution. We eventually need to fold
  1372. * mpage_da_submit_io() into this function and then call
  1373. * ext4_map_blocks() multiple times in a loop
  1374. */
  1375. if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
  1376. goto flush_it;
  1377. /* check if thereserved journal credits might overflow */
  1378. if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
  1379. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  1380. /*
  1381. * With non-extent format we are limited by the journal
  1382. * credit available. Total credit needed to insert
  1383. * nrblocks contiguous blocks is dependent on the
  1384. * nrblocks. So limit nrblocks.
  1385. */
  1386. goto flush_it;
  1387. } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
  1388. EXT4_MAX_TRANS_DATA) {
  1389. /*
  1390. * Adding the new buffer_head would make it cross the
  1391. * allowed limit for which we have journal credit
  1392. * reserved. So limit the new bh->b_size
  1393. */
  1394. b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
  1395. mpd->inode->i_blkbits;
  1396. /* we will do mpage_da_submit_io in the next loop */
  1397. }
  1398. }
  1399. /*
  1400. * First block in the extent
  1401. */
  1402. if (mpd->b_size == 0) {
  1403. mpd->b_blocknr = logical;
  1404. mpd->b_size = b_size;
  1405. mpd->b_state = b_state & BH_FLAGS;
  1406. return;
  1407. }
  1408. next = mpd->b_blocknr + nrblocks;
  1409. /*
  1410. * Can we merge the block to our big extent?
  1411. */
  1412. if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
  1413. mpd->b_size += b_size;
  1414. return;
  1415. }
  1416. flush_it:
  1417. /*
  1418. * We couldn't merge the block to our extent, so we
  1419. * need to flush current extent and start new one
  1420. */
  1421. mpage_da_map_and_submit(mpd);
  1422. return;
  1423. }
  1424. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  1425. {
  1426. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  1427. }
  1428. /*
  1429. * This is a special get_blocks_t callback which is used by
  1430. * ext4_da_write_begin(). It will either return mapped block or
  1431. * reserve space for a single block.
  1432. *
  1433. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1434. * We also have b_blocknr = -1 and b_bdev initialized properly
  1435. *
  1436. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1437. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1438. * initialized properly.
  1439. */
  1440. static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1441. struct buffer_head *bh, int create)
  1442. {
  1443. struct ext4_map_blocks map;
  1444. int ret = 0;
  1445. sector_t invalid_block = ~((sector_t) 0xffff);
  1446. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1447. invalid_block = ~0;
  1448. BUG_ON(create == 0);
  1449. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1450. map.m_lblk = iblock;
  1451. map.m_len = 1;
  1452. /*
  1453. * first, we need to know whether the block is allocated already
  1454. * preallocated blocks are unmapped but should treated
  1455. * the same as allocated blocks.
  1456. */
  1457. ret = ext4_map_blocks(NULL, inode, &map, 0);
  1458. if (ret < 0)
  1459. return ret;
  1460. if (ret == 0) {
  1461. if (buffer_delay(bh))
  1462. return 0; /* Not sure this could or should happen */
  1463. /*
  1464. * XXX: __block_write_begin() unmaps passed block, is it OK?
  1465. */
  1466. ret = ext4_da_reserve_space(inode, iblock);
  1467. if (ret)
  1468. /* not enough space to reserve */
  1469. return ret;
  1470. map_bh(bh, inode->i_sb, invalid_block);
  1471. set_buffer_new(bh);
  1472. set_buffer_delay(bh);
  1473. return 0;
  1474. }
  1475. map_bh(bh, inode->i_sb, map.m_pblk);
  1476. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  1477. if (buffer_unwritten(bh)) {
  1478. /* A delayed write to unwritten bh should be marked
  1479. * new and mapped. Mapped ensures that we don't do
  1480. * get_block multiple times when we write to the same
  1481. * offset and new ensures that we do proper zero out
  1482. * for partial write.
  1483. */
  1484. set_buffer_new(bh);
  1485. set_buffer_mapped(bh);
  1486. }
  1487. return 0;
  1488. }
  1489. /*
  1490. * This function is used as a standard get_block_t calback function
  1491. * when there is no desire to allocate any blocks. It is used as a
  1492. * callback function for block_write_begin() and block_write_full_page().
  1493. * These functions should only try to map a single block at a time.
  1494. *
  1495. * Since this function doesn't do block allocations even if the caller
  1496. * requests it by passing in create=1, it is critically important that
  1497. * any caller checks to make sure that any buffer heads are returned
  1498. * by this function are either all already mapped or marked for
  1499. * delayed allocation before calling block_write_full_page(). Otherwise,
  1500. * b_blocknr could be left unitialized, and the page write functions will
  1501. * be taken by surprise.
  1502. */
  1503. static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
  1504. struct buffer_head *bh_result, int create)
  1505. {
  1506. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  1507. return _ext4_get_block(inode, iblock, bh_result, 0);
  1508. }
  1509. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1510. {
  1511. get_bh(bh);
  1512. return 0;
  1513. }
  1514. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1515. {
  1516. put_bh(bh);
  1517. return 0;
  1518. }
  1519. static int __ext4_journalled_writepage(struct page *page,
  1520. unsigned int len)
  1521. {
  1522. struct address_space *mapping = page->mapping;
  1523. struct inode *inode = mapping->host;
  1524. struct buffer_head *page_bufs;
  1525. handle_t *handle = NULL;
  1526. int ret = 0;
  1527. int err;
  1528. ClearPageChecked(page);
  1529. page_bufs = page_buffers(page);
  1530. BUG_ON(!page_bufs);
  1531. walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
  1532. /* As soon as we unlock the page, it can go away, but we have
  1533. * references to buffers so we are safe */
  1534. unlock_page(page);
  1535. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  1536. if (IS_ERR(handle)) {
  1537. ret = PTR_ERR(handle);
  1538. goto out;
  1539. }
  1540. BUG_ON(!ext4_handle_valid(handle));
  1541. ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1542. do_journal_get_write_access);
  1543. err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1544. write_end_fn);
  1545. if (ret == 0)
  1546. ret = err;
  1547. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1548. err = ext4_journal_stop(handle);
  1549. if (!ret)
  1550. ret = err;
  1551. walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
  1552. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1553. out:
  1554. return ret;
  1555. }
  1556. static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
  1557. static void ext4_end_io_buffer_write(struct buffer_head *