migrate.c 17 KB

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