migrate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 = sb_bread(inode->i_sb, pblock);
  110. if (!bh)
  111. return -EIO;
  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 = sb_bread(inode->i_sb, pblock);
  135. if (!bh)
  136. return -EIO;
  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 = sb_bread(inode->i_sb, pblock);
  161. if (!bh)
  162. return -EIO;
  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 = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  202. if (!bh)
  203. return -EIO;
  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 = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  229. if (!bh)
  230. return -EIO;
  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 = sb_bread(inode->i_sb, block);
  343. if (!bh)
  344. return -EIO;
  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. handle_t *handle;
  385. int retval = 0, i;
  386. __le32 *i_data;
  387. struct ext4_inode_info *ei;
  388. struct inode *tmp_inode = NULL;
  389. struct migrate_struct lb;
  390. unsigned long max_entries;
  391. __u32 goal;
  392. uid_t owner[2];
  393. /*
  394. * If the filesystem does not support extents, or the inode
  395. * already is extent-based, error out.
  396. */
  397. if (!ext4_has_feature_extents(inode->i_sb) ||
  398. (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  399. return -EINVAL;
  400. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  401. /*
  402. * don't migrate fast symlink
  403. */
  404. return retval;
  405. /*
  406. * Worst case we can touch the allocation bitmaps, a bgd
  407. * block, and a block to link in the orphan list. We do need
  408. * need to worry about credits for modifying the quota inode.
  409. */
  410. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE,
  411. 4 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
  412. if (IS_ERR(handle)) {
  413. retval = PTR_ERR(handle);
  414. return retval;
  415. }
  416. goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
  417. EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
  418. owner[0] = i_uid_read(inode);
  419. owner[1] = i_gid_read(inode);
  420. tmp_inode = ext4_new_inode(handle, d_inode(inode->i_sb->s_root),
  421. S_IFREG, NULL, goal, owner, 0);
  422. if (IS_ERR(tmp_inode)) {
  423. retval = PTR_ERR(tmp_inode);
  424. ext4_journal_stop(handle);
  425. return retval;
  426. }
  427. i_size_write(tmp_inode, i_size_read(inode));
  428. /*
  429. * Set the i_nlink to zero so it will be deleted later
  430. * when we drop inode reference.
  431. */
  432. clear_nlink(tmp_inode);
  433. ext4_ext_tree_init(handle, tmp_inode);
  434. ext4_orphan_add(handle, tmp_inode);
  435. ext4_journal_stop(handle);
  436. /*
  437. * start with one credit accounted for
  438. * superblock modification.
  439. *
  440. * For the tmp_inode we already have committed the
  441. * transaction that created the inode. Later as and
  442. * when we add extents we extent the journal
  443. */
  444. /*
  445. * Even though we take i_mutex we can still cause block
  446. * allocation via mmap write to holes. If we have allocated
  447. * new blocks we fail migrate. New block allocation will
  448. * clear EXT4_STATE_EXT_MIGRATE flag. The flag is updated
  449. * with i_data_sem held to prevent racing with block
  450. * allocation.
  451. */
  452. down_read(&EXT4_I(inode)->i_data_sem);
  453. ext4_set_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  454. up_read((&EXT4_I(inode)->i_data_sem));
  455. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
  456. if (IS_ERR(handle)) {
  457. /*
  458. * It is impossible to update on-disk structures without
  459. * a handle, so just rollback in-core changes and live other
  460. * work to orphan_list_cleanup()
  461. */
  462. ext4_orphan_del(NULL, tmp_inode);
  463. retval = PTR_ERR(handle);
  464. goto out;
  465. }
  466. ei = EXT4_I(inode);
  467. i_data = ei->i_data;
  468. memset(&lb, 0, sizeof(lb));
  469. /* 32 bit block address 4 bytes */
  470. max_entries = inode->i_sb->s_blocksize >> 2;
  471. for (i = 0; i < EXT4_NDIR_BLOCKS; i++) {
  472. if (i_data[i]) {
  473. retval = update_extent_range(handle, tmp_inode,
  474. le32_to_cpu(i_data[i]), &lb);
  475. if (retval)
  476. goto err_out;
  477. } else
  478. lb.curr_block++;
  479. }
  480. if (i_data[EXT4_IND_BLOCK]) {
  481. retval = update_ind_extent_range(handle, tmp_inode,
  482. le32_to_cpu(i_data[EXT4_IND_BLOCK]), &lb);
  483. if (retval)
  484. goto err_out;
  485. } else
  486. lb.curr_block += max_entries;
  487. if (i_data[EXT4_DIND_BLOCK]) {
  488. retval = update_dind_extent_range(handle, tmp_inode,
  489. le32_to_cpu(i_data[EXT4_DIND_BLOCK]), &lb);
  490. if (retval)
  491. goto err_out;
  492. } else
  493. lb.curr_block += max_entries * max_entries;
  494. if (i_data[EXT4_TIND_BLOCK]) {
  495. retval = update_tind_extent_range(handle, tmp_inode,
  496. le32_to_cpu(i_data[EXT4_TIND_BLOCK]), &lb);
  497. if (retval)
  498. goto err_out;
  499. }
  500. /*
  501. * Build the last extent
  502. */
  503. retval = finish_range(handle, tmp_inode, &lb);
  504. err_out:
  505. if (retval)
  506. /*
  507. * Failure case delete the extent information with the
  508. * tmp_inode
  509. */
  510. free_ext_block(handle, tmp_inode);
  511. else {
  512. retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
  513. if (retval)
  514. /*
  515. * if we fail to swap inode data free the extent
  516. * details of the tmp inode
  517. */
  518. free_ext_block(handle, tmp_inode);
  519. }
  520. /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
  521. if (ext4_journal_extend(handle, 1) != 0)
  522. ext4_journal_restart(handle, 1);
  523. /*
  524. * Mark the tmp_inode as of size zero
  525. */
  526. i_size_write(tmp_inode, 0);
  527. /*
  528. * set the i_blocks count to zero
  529. * so that the ext4_evict_inode() does the
  530. * right job
  531. *
  532. * We don't need to take the i_lock because
  533. * the inode is not visible to user space.
  534. */
  535. tmp_inode->i_blocks = 0;
  536. /* Reset the extent details */
  537. ext4_ext_tree_init(handle, tmp_inode);
  538. ext4_journal_stop(handle);
  539. out:
  540. unlock_new_inode(tmp_inode);
  541. iput(tmp_inode);
  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_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  551. struct ext4_inode_info *ei = EXT4_I(inode);
  552. struct ext4_extent *ex;
  553. unsigned int i, len;
  554. ext4_lblk_t start, end;
  555. ext4_fsblk_t blk;
  556. handle_t *handle;
  557. int ret;
  558. if (!ext4_has_feature_extents(inode->i_sb) ||
  559. (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  560. return -EINVAL;
  561. if (ext4_has_feature_bigalloc(inode->i_sb))
  562. return -EOPNOTSUPP;
  563. /*
  564. * In order to get correct extent info, force all delayed allocation
  565. * blocks to be allocated, otherwise delayed allocation blocks may not
  566. * be reflected and bypass the checks on extent header.
  567. */
  568. if (test_opt(inode->i_sb, DELALLOC))
  569. ext4_alloc_da_blocks(inode);
  570. handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
  571. if (IS_ERR(handle))
  572. return PTR_ERR(handle);
  573. down_write(&EXT4_I(inode)->i_data_sem);
  574. ret = ext4_ext_check_inode(inode);
  575. if (ret)
  576. goto errout;
  577. eh = ext_inode_hdr(inode);
  578. ex = EXT_FIRST_EXTENT(eh);
  579. if (ext4_blocks_count(es) > EXT4_MAX_BLOCK_FILE_PHYS ||
  580. eh->eh_depth != 0 || le16_to_cpu(eh->eh_entries) > 1) {
  581. ret = -EOPNOTSUPP;
  582. goto errout;
  583. }
  584. if (eh->eh_entries == 0)
  585. blk = len = start = end = 0;
  586. else {
  587. len = le16_to_cpu(ex->ee_len);
  588. blk = ext4_ext_pblock(ex);
  589. start = le32_to_cpu(ex->ee_block);
  590. end = start + len - 1;
  591. if (end >= EXT4_NDIR_BLOCKS) {
  592. ret = -EOPNOTSUPP;
  593. goto errout;
  594. }
  595. }
  596. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  597. memset(ei->i_data, 0, sizeof(ei->i_data));
  598. for (i = start; i <= end; i++)
  599. ei->i_data[i] = cpu_to_le32(blk++);
  600. ext4_mark_inode_dirty(handle, inode);
  601. errout:
  602. ext4_journal_stop(handle);
  603. up_write(&EXT4_I(inode)->i_data_sem);
  604. return ret;
  605. }