migrate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright IBM Corporation, 2007
  4. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  5. *
  6. */
  7. #include <linux/slab.h>
  8. #include "ext4_jbd2.h"
  9. #include "ext4_extents.h"
  10. /*
  11. * The contiguous blocks details which can be
  12. * represented by a single extent
  13. */
  14. struct migrate_struct {
  15. ext4_lblk_t first_block, last_block, curr_block;
  16. ext4_fsblk_t first_pblock, last_pblock;
  17. };
  18. static int finish_range(handle_t *handle, struct inode *inode,
  19. struct migrate_struct *lb)
  20. {
  21. int retval = 0, needed;
  22. struct ext4_extent newext;
  23. struct ext4_ext_path *path;
  24. if (lb->first_pblock == 0)
  25. return 0;
  26. /* Add the extent to temp inode*/
  27. newext.ee_block = cpu_to_le32(lb->first_block);
  28. newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block + 1);
  29. ext4_ext_store_pblock(&newext, lb->first_pblock);
  30. /* Locking only for convenience since we are operating on temp inode */
  31. down_write(&EXT4_I(inode)->i_data_sem);
  32. path = ext4_find_extent(inode, lb->first_block, NULL, 0);
  33. if (IS_ERR(path)) {
  34. retval = PTR_ERR(path);
  35. goto err_out;
  36. }
  37. /*
  38. * Calculate the credit needed to inserting this extent
  39. * Since we are doing this in loop we may accumulate extra
  40. * credit. But below we try to not accumulate too much
  41. * of them by restarting the journal.
  42. */
  43. needed = ext4_ext_calc_credits_for_single_extent(inode,
  44. lb->last_block - lb->first_block + 1, path);
  45. retval = ext4_datasem_ensure_credits(handle, inode, needed, needed, 0);
  46. if (retval < 0)
  47. goto err_out;
  48. path = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
  49. if (IS_ERR(path))
  50. retval = PTR_ERR(path);
  51. err_out:
  52. up_write((&EXT4_I(inode)->i_data_sem));
  53. ext4_free_ext_path(path);
  54. lb->first_pblock = 0;
  55. return retval;
  56. }
  57. static int update_extent_range(handle_t *handle, struct inode *inode,
  58. ext4_fsblk_t pblock, struct migrate_struct *lb)
  59. {
  60. int retval;
  61. /*
  62. * See if we can add on to the existing range (if it exists)
  63. */
  64. if (lb->first_pblock &&
  65. (lb->last_pblock+1 == pblock) &&
  66. (lb->last_block+1 == lb->curr_block)) {
  67. lb->last_pblock = pblock;
  68. lb->last_block = lb->curr_block;
  69. lb->curr_block++;
  70. return 0;
  71. }
  72. /*
  73. * Start a new range.
  74. */
  75. retval = finish_range(handle, inode, lb);
  76. lb->first_pblock = lb->last_pblock = pblock;
  77. lb->first_block = lb->last_block = lb->curr_block;
  78. lb->curr_block++;
  79. return retval;
  80. }
  81. static int update_ind_extent_range(handle_t *handle, struct inode *inode,
  82. ext4_fsblk_t pblock,
  83. struct migrate_struct *lb)
  84. {
  85. struct buffer_head *bh;
  86. __le32 *i_data;
  87. int i, retval = 0;
  88. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  89. bh = ext4_sb_bread(inode->i_sb, pblock, 0);
  90. if (IS_ERR(bh))
  91. return PTR_ERR(bh);
  92. i_data = (__le32 *)bh->b_data;
  93. for (i = 0; i < max_entries; i++) {
  94. if (i_data[i]) {
  95. retval = update_extent_range(handle, inode,
  96. le32_to_cpu(i_data[i]), lb);
  97. if (retval)
  98. break;
  99. } else {
  100. lb->curr_block++;
  101. }
  102. }
  103. put_bh(bh);
  104. return retval;
  105. }
  106. static int update_dind_extent_range(handle_t *handle, struct inode *inode,
  107. ext4_fsblk_t pblock,
  108. struct migrate_struct *lb)
  109. {
  110. struct buffer_head *bh;
  111. __le32 *i_data;
  112. int i, retval = 0;
  113. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  114. bh = ext4_sb_bread(inode->i_sb, pblock, 0);
  115. if (IS_ERR(bh))
  116. return PTR_ERR(bh);
  117. i_data = (__le32 *)bh->b_data;
  118. for (i = 0; i < max_entries; i++) {
  119. if (i_data[i]) {
  120. retval = update_ind_extent_range(handle, inode,
  121. le32_to_cpu(i_data[i]), lb);
  122. if (retval)
  123. break;
  124. } else {
  125. /* Only update the file block number */
  126. lb->curr_block += max_entries;
  127. }
  128. }
  129. put_bh(bh);
  130. return retval;
  131. }
  132. static int update_tind_extent_range(handle_t *handle, struct inode *inode,
  133. ext4_fsblk_t pblock,
  134. struct migrate_struct *lb)
  135. {
  136. struct buffer_head *bh;
  137. __le32 *i_data;
  138. int i, retval = 0;
  139. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  140. bh = ext4_sb_bread(inode->i_sb, pblock, 0);
  141. if (IS_ERR(bh))
  142. return PTR_ERR(bh);
  143. i_data = (__le32 *)bh->b_data;
  144. for (i = 0; i < max_entries; i++) {
  145. if (i_data[i]) {
  146. retval = update_dind_extent_range(handle, inode,
  147. le32_to_cpu(i_data[i]), lb);
  148. if (retval)
  149. break;
  150. } else {
  151. /* Only update the file block number */
  152. lb->curr_block += max_entries * max_entries;
  153. }
  154. }
  155. put_bh(bh);
  156. return retval;
  157. }
  158. static int free_dind_blocks(handle_t *handle,
  159. struct inode *inode, __le32 i_data)
  160. {
  161. int i;
  162. __le32 *tmp_idata;
  163. struct buffer_head *bh;
  164. struct super_block *sb = inode->i_sb;
  165. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  166. int err;
  167. bh = ext4_sb_bread(sb, le32_to_cpu(i_data), 0);
  168. if (IS_ERR(bh))
  169. return PTR_ERR(bh);
  170. tmp_idata = (__le32 *)bh->b_data;
  171. for (i = 0; i < max_entries; i++) {
  172. if (tmp_idata[i]) {
  173. err = ext4_journal_ensure_credits(handle,
  174. EXT4_RESERVE_TRANS_BLOCKS,
  175. ext4_free_metadata_revoke_credits(sb, 1));
  176. if (err < 0) {
  177. put_bh(bh);
  178. return err;
  179. }
  180. ext4_free_blocks(handle, inode, NULL,
  181. le32_to_cpu(tmp_idata[i]), 1,
  182. EXT4_FREE_BLOCKS_METADATA |
  183. EXT4_FREE_BLOCKS_FORGET);
  184. }
  185. }
  186. put_bh(bh);
  187. err = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
  188. ext4_free_metadata_revoke_credits(sb, 1));
  189. if (err < 0)
  190. return err;
  191. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  192. EXT4_FREE_BLOCKS_METADATA |
  193. EXT4_FREE_BLOCKS_FORGET);
  194. return 0;
  195. }
  196. static int free_tind_blocks(handle_t *handle,
  197. struct inode *inode, __le32 i_data)
  198. {
  199. int i, retval = 0;
  200. __le32 *tmp_idata;
  201. struct buffer_head *bh;
  202. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  203. bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0);
  204. if (IS_ERR(bh))
  205. return PTR_ERR(bh);
  206. tmp_idata = (__le32 *)bh->b_data;
  207. for (i = 0; i < max_entries; i++) {
  208. if (tmp_idata[i]) {
  209. retval = free_dind_blocks(handle,
  210. inode, tmp_idata[i]);
  211. if (retval) {
  212. put_bh(bh);
  213. return retval;
  214. }
  215. }
  216. }
  217. put_bh(bh);
  218. retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
  219. ext4_free_metadata_revoke_credits(inode->i_sb, 1));
  220. if (retval < 0)
  221. return retval;
  222. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  223. EXT4_FREE_BLOCKS_METADATA |
  224. EXT4_FREE_BLOCKS_FORGET);
  225. return 0;
  226. }
  227. static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
  228. {
  229. int retval;
  230. /* ei->i_data[EXT4_IND_BLOCK] */
  231. if (i_data[0]) {
  232. retval = ext4_journal_ensure_credits(handle,
  233. EXT4_RESERVE_TRANS_BLOCKS,
  234. ext4_free_metadata_revoke_credits(inode->i_sb, 1));
  235. if (retval < 0)
  236. return retval;
  237. ext4_free_blocks(handle, inode, NULL,
  238. le32_to_cpu(i_data[0]), 1,
  239. EXT4_FREE_BLOCKS_METADATA |
  240. EXT4_FREE_BLOCKS_FORGET);
  241. }
  242. /* ei->i_data[EXT4_DIND_BLOCK] */
  243. if (i_data[1]) {
  244. retval = free_dind_blocks(handle, inode, i_data[1]);
  245. if (retval)
  246. return retval;
  247. }
  248. /* ei->i_data[EXT4_TIND_BLOCK] */
  249. if (i_data[2]) {
  250. retval = free_tind_blocks(handle, inode, i_data[2]);
  251. if (retval)
  252. return retval;
  253. }
  254. return 0;
  255. }
  256. static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
  257. struct inode *tmp_inode)
  258. {
  259. int retval, retval2 = 0;
  260. __le32 i_data[3];
  261. struct ext4_inode_info *ei = EXT4_I(inode);
  262. struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
  263. /*
  264. * One credit accounted for writing the
  265. * i_data field of the original inode
  266. */
  267. retval = ext4_journal_ensure_credits(handle, 1, 0);
  268. if (retval < 0)
  269. goto err_out;
  270. i_data[0] = ei->i_data[EXT4_IND_BLOCK];
  271. i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
  272. i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
  273. down_write(&EXT4_I(inode)->i_data_sem);
  274. /*
  275. * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
  276. * happened after we started the migrate. We need to
  277. * fail the migrate
  278. */
  279. if (!ext4_test_inode_state(inode, EXT4_STATE_EXT_MIGRATE)) {
  280. retval = -EAGAIN;
  281. up_write(&EXT4_I(inode)->i_data_sem);
  282. goto err_out;
  283. } else
  284. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  285. /*
  286. * We have the extent map build with the tmp inode.
  287. * Now copy the i_data across
  288. */
  289. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  290. memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
  291. /*
  292. * Update i_blocks with the new blocks that got
  293. * allocated while adding extents for extent index
  294. * blocks.
  295. *
  296. * While converting to extents we need not
  297. * update the original inode i_blocks for extent blocks
  298. * via quota APIs. The quota update happened via tmp_inode already.
  299. */
  300. spin_lock(&inode->i_lock);
  301. inode->i_blocks += tmp_inode->i_blocks;
  302. spin_unlock(&inode->i_lock);
  303. up_write(&EXT4_I(inode)->i_data_sem);
  304. /*
  305. * We mark the inode dirty after, because we decrement the
  306. * i_blocks when freeing the indirect meta-data blocks
  307. */
  308. retval = free_ind_block(handle, inode, i_data);
  309. retval2 = ext4_mark_inode_dirty(handle, inode);
  310. if (unlikely(retval2 && !retval))
  311. retval = retval2;
  312. err_out:
  313. return retval;
  314. }
  315. static int free_ext_idx(handle_t *handle, struct inode *inode,
  316. struct ext4_extent_idx *ix)
  317. {
  318. int i, retval = 0;
  319. ext4_fsblk_t block;
  320. struct buffer_head *bh;
  321. struct ext4_extent_header *eh;
  322. block = ext4_idx_pblock(ix);
  323. bh = ext4_sb_bread(inode->i_sb, block, 0);
  324. if (IS_ERR(bh))
  325. return PTR_ERR(bh);
  326. eh = (struct ext4_extent_header *)bh->b_data;
  327. if (eh->eh_depth != 0) {
  328. ix = EXT_FIRST_INDEX(eh);
  329. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  330. retval = free_ext_idx(handle, inode, ix);
  331. if (retval) {
  332. put_bh(bh);
  333. return retval;
  334. }
  335. }
  336. }
  337. put_bh(bh);
  338. retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS,
  339. ext4_free_metadata_revoke_credits(inode->i_sb, 1));
  340. if (retval < 0)
  341. return retval;
  342. ext4_free_blocks(handle, inode, NULL, block, 1,
  343. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  344. return 0;
  345. }
  346. /*
  347. * Free the extent meta data blocks only
  348. */
  349. static int free_ext_block(handle_t *handle, struct inode *inode)
  350. {
  351. int i, retval = 0;
  352. struct ext4_inode_info *ei = EXT4_I(inode);
  353. struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
  354. struct ext4_extent_idx *ix;
  355. if (eh->eh_depth == 0)
  356. /*
  357. * No extra blocks allocated for extent meta data
  358. */
  359. return 0;
  360. ix = EXT_FIRST_INDEX(eh);
  361. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  362. retval = free_ext_idx(handle, inode, ix);
  363. if (retval)
  364. return retval;
  365. }
  366. return retval;
  367. }
  368. int ext4_ext_migrate(struct inode *inode)
  369. {
  370. handle_t *handle;
  371. int retval = 0, i;
  372. __le32 *i_data;
  373. struct ext4_inode_info *ei;
  374. struct inode *tmp_inode = NULL;
  375. struct migrate_struct lb;
  376. unsigned long max_entries;
  377. __u32 goal, tmp_csum_seed;
  378. uid_t owner[2];
  379. int alloc_ctx;
  380. /*
  381. * If the filesystem does not support extents, or the inode
  382. * already is extent-based, error out.
  383. */
  384. if (!ext4_has_feature_extents(inode->i_sb) ||
  385. ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
  386. ext4_has_inline_data(inode))
  387. return -EINVAL;
  388. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  389. /*
  390. * don't migrate fast symlink
  391. */
  392. return retval;
  393. alloc_ctx = ext4_writepages_down_write(inode->i_sb);
  394. /*
  395. * Worst case we can touch the allocation bitmaps and a block
  396. * group descriptor block. We do need to worry about
  397. * credits for modifying the quota inode.
  398. */
  399. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE,
  400. 3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
  401. if (IS_ERR(handle)) {
  402. retval = PTR_ERR(handle);
  403. goto out_unlock;
  404. }
  405. /*
  406. * This operation rewrites the inode's block mapping layout
  407. * (indirect to extents) and is not tracked in the fast commit
  408. * log, so disable fast commits for this transaction.
  409. */
  410. ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle);
  411. goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
  412. EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
  413. owner[0] = i_uid_read(inode);
  414. owner[1] = i_gid_read(inode);
  415. tmp_inode = ext4_new_inode(handle, d_inode(inode->i_sb->s_root),
  416. S_IFREG, NULL, goal, owner, 0);
  417. if (IS_ERR(tmp_inode)) {
  418. retval = PTR_ERR(tmp_inode);
  419. ext4_journal_stop(handle);
  420. goto out_unlock;
  421. }
  422. /*
  423. * Use the correct seed for checksum (i.e. the seed from 'inode'). This
  424. * is so that the metadata blocks will have the correct checksum after
  425. * the migration.
  426. */
  427. ei = EXT4_I(inode);
  428. tmp_csum_seed = EXT4_I(tmp_inode)->i_csum_seed;
  429. EXT4_I(tmp_inode)->i_csum_seed = ei->i_csum_seed;
  430. i_size_write(tmp_inode, i_size_read(inode));
  431. /*
  432. * Set the i_nlink to zero so it will be deleted later
  433. * when we drop inode reference.
  434. */
  435. clear_nlink(tmp_inode);
  436. ext4_ext_tree_init(handle, tmp_inode);
  437. ext4_journal_stop(handle);
  438. /*
  439. * start with one credit accounted for
  440. * superblock modification.
  441. *
  442. * For the tmp_inode we already have committed the
  443. * transaction that created the inode. Later as and
  444. * when we add extents we extent the journal
  445. */
  446. /*
  447. * Even though we take i_rwsem we can still cause block
  448. * allocation via mmap write to holes. If we have allocated
  449. * new blocks we fail migrate. New block allocation will
  450. * clear EXT4_STATE_EXT_MIGRATE flag. The flag is updated
  451. * with i_data_sem held to prevent racing with block
  452. * allocation.
  453. */
  454. down_read(&EXT4_I(inode)->i_data_sem);
  455. ext4_set_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  456. up_read((&EXT4_I(inode)->i_data_sem));
  457. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
  458. if (IS_ERR(handle)) {
  459. retval = PTR_ERR(handle);
  460. goto out_tmp_inode;
  461. }
  462. i_data = ei->i_data;
  463. memset(&lb, 0, sizeof(lb));
  464. /* 32 bit block address 4 bytes */
  465. max_entries = inode->i_sb->s_blocksize >> 2;
  466. for (i = 0; i < EXT4_NDIR_BLOCKS; i++) {
  467. if (i_data[i]) {
  468. retval = update_extent_range(handle, tmp_inode,
  469. le32_to_cpu(i_data[i]), &lb);
  470. if (retval)
  471. goto err_out;
  472. } else
  473. lb.curr_block++;
  474. }
  475. if (i_data[EXT4_IND_BLOCK]) {
  476. retval = update_ind_extent_range(handle, tmp_inode,
  477. le32_to_cpu(i_data[EXT4_IND_BLOCK]), &lb);
  478. if (retval)
  479. goto err_out;
  480. } else
  481. lb.curr_block += max_entries;
  482. if (i_data[EXT4_DIND_BLOCK]) {
  483. retval = update_dind_extent_range(handle, tmp_inode,
  484. le32_to_cpu(i_data[EXT4_DIND_BLOCK]), &lb);
  485. if (retval)
  486. goto err_out;
  487. } else
  488. lb.curr_block += max_entries * max_entries;
  489. if (i_data[EXT4_TIND_BLOCK]) {
  490. retval = update_tind_extent_range(handle, tmp_inode,
  491. le32_to_cpu(i_data[EXT4_TIND_BLOCK]), &lb);
  492. if (retval)
  493. goto err_out;
  494. }
  495. /*
  496. * Build the last extent
  497. */
  498. retval = finish_range(handle, tmp_inode, &lb);
  499. err_out:
  500. if (retval)
  501. /*
  502. * Failure case delete the extent information with the
  503. * tmp_inode
  504. */
  505. free_ext_block(handle, tmp_inode);
  506. else {
  507. retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
  508. if (retval)
  509. /*
  510. * if we fail to swap inode data free the extent
  511. * details of the tmp inode
  512. */
  513. free_ext_block(handle, tmp_inode);
  514. }
  515. /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
  516. retval = ext4_journal_ensure_credits(handle, 1, 0);
  517. if (retval < 0)
  518. goto out_stop;
  519. /*
  520. * Mark the tmp_inode as of size zero
  521. */
  522. i_size_write(tmp_inode, 0);
  523. /*
  524. * set the i_blocks count to zero
  525. * so that the ext4_evict_inode() does the
  526. * right job
  527. *
  528. * We don't need to take the i_lock because
  529. * the inode is not visible to user space.
  530. */
  531. tmp_inode->i_blocks = 0;
  532. EXT4_I(tmp_inode)->i_csum_seed = tmp_csum_seed;
  533. /* Reset the extent details */
  534. ext4_ext_tree_init(handle, tmp_inode);
  535. out_stop:
  536. ext4_journal_stop(handle);
  537. out_tmp_inode:
  538. unlock_new_inode(tmp_inode);
  539. iput(tmp_inode);
  540. out_unlock:
  541. ext4_writepages_up_write(inode->i_sb, alloc_ctx);
  542. return retval;
  543. }
  544. /*
  545. * Migrate a simple extent-based inode to use the i_blocks[] array
  546. */
  547. int ext4_ind_migrate(struct inode *inode)
  548. {
  549. struct ext4_extent_header *eh;
  550. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  551. struct ext4_super_block *es = sbi->s_es;
  552. struct ext4_inode_info *ei = EXT4_I(inode);
  553. struct ext4_extent *ex;
  554. unsigned int i, len;
  555. ext4_lblk_t start, end;
  556. ext4_fsblk_t blk;
  557. handle_t *handle;
  558. int ret, ret2 = 0;
  559. int alloc_ctx;
  560. if (!ext4_has_feature_extents(inode->i_sb) ||
  561. (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  562. return -EINVAL;
  563. if (ext4_has_feature_bigalloc(inode->i_sb))
  564. return -EOPNOTSUPP;
  565. /*
  566. * In order to get correct extent info, force all delayed allocation
  567. * blocks to be allocated, otherwise delayed allocation blocks may not
  568. * be reflected and bypass the checks on extent header.
  569. */
  570. if (test_opt(inode->i_sb, DELALLOC))
  571. ext4_alloc_da_blocks(inode);
  572. alloc_ctx = ext4_writepages_down_write(inode->i_sb);
  573. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
  574. if (IS_ERR(handle)) {
  575. ret = PTR_ERR(handle);
  576. goto out_unlock;
  577. }
  578. /*
  579. * This operation rewrites the inode's block mapping layout
  580. * (extents to indirect blocks) and is not tracked in the fast
  581. * commit log, so disable fast commits for this transaction.
  582. */
  583. ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle);
  584. down_write(&EXT4_I(inode)->i_data_sem);
  585. ret = ext4_ext_check_inode(inode);
  586. if (ret)
  587. goto errout;
  588. eh = ext_inode_hdr(inode);
  589. ex = EXT_FIRST_EXTENT(eh);
  590. if (ext4_blocks_count(es) > EXT4_MAX_BLOCK_FILE_PHYS ||
  591. eh->eh_depth != 0 || le16_to_cpu(eh->eh_entries) > 1) {
  592. ret = -EOPNOTSUPP;
  593. goto errout;
  594. }
  595. if (eh->eh_entries == 0)
  596. blk = len = start = end = 0;
  597. else {
  598. len = le16_to_cpu(ex->ee_len);
  599. blk = ext4_ext_pblock(ex);
  600. start = le32_to_cpu(ex->ee_block);
  601. end = start + len - 1;
  602. if (end >= EXT4_NDIR_BLOCKS) {
  603. ret = -EOPNOTSUPP;
  604. goto errout;
  605. }
  606. }
  607. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  608. memset(ei->i_data, 0, sizeof(ei->i_data));
  609. for (i = start; i <= end; i++)
  610. ei->i_data[i] = cpu_to_le32(blk++);
  611. ret2 = ext4_mark_inode_dirty(handle, inode);
  612. if (unlikely(ret2 && !ret))
  613. ret = ret2;
  614. errout:
  615. up_write(&EXT4_I(inode)->i_data_sem);
  616. ext4_journal_stop(handle);
  617. out_unlock:
  618. ext4_writepages_up_write(inode->i_sb, alloc_ctx);
  619. return ret;
  620. }