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