migrate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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/module.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 list_blocks_struct {
  22. ext4_lblk_t first_block, last_block;
  23. ext4_fsblk_t first_pblock, last_pblock;
  24. };
  25. static int finish_range(handle_t *handle, struct inode *inode,
  26. struct list_blocks_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. path = ext4_ext_find_extent(inode, lb->first_block, NULL);
  38. if (IS_ERR(path)) {
  39. retval = PTR_ERR(path);
  40. path = NULL;
  41. goto err_out;
  42. }
  43. /*
  44. * Calculate the credit needed to inserting this extent
  45. * Since we are doing this in loop we may accumalate extra
  46. * credit. But below we try to not accumalate too much
  47. * of them by restarting the journal.
  48. */
  49. needed = ext4_ext_calc_credits_for_single_extent(inode,
  50. lb->last_block - lb->first_block + 1, path);
  51. /*
  52. * Make sure the credit we accumalated is not really high
  53. */
  54. if (needed && handle->h_buffer_credits >= EXT4_RESERVE_TRANS_BLOCKS) {
  55. retval = ext4_journal_restart(handle, needed);
  56. if (retval)
  57. goto err_out;
  58. } else if (needed) {
  59. retval = ext4_journal_extend(handle, needed);
  60. if (retval) {
  61. /*
  62. * IF not able to extend the journal restart the journal
  63. */
  64. retval = ext4_journal_restart(handle, needed);
  65. if (retval)
  66. goto err_out;
  67. }
  68. }
  69. retval = ext4_ext_insert_extent(handle, inode, path, &newext);
  70. err_out:
  71. if (path) {
  72. ext4_ext_drop_refs(path);
  73. kfree(path);
  74. }
  75. lb->first_pblock = 0;
  76. return retval;
  77. }
  78. static int update_extent_range(handle_t *handle, struct inode *inode,
  79. ext4_fsblk_t pblock, ext4_lblk_t blk_num,
  80. struct list_blocks_struct *lb)
  81. {
  82. int retval;
  83. /*
  84. * See if we can add on to the existing range (if it exists)
  85. */
  86. if (lb->first_pblock &&
  87. (lb->last_pblock+1 == pblock) &&
  88. (lb->last_block+1 == blk_num)) {
  89. lb->last_pblock = pblock;
  90. lb->last_block = blk_num;
  91. return 0;
  92. }
  93. /*
  94. * Start a new range.
  95. */
  96. retval = finish_range(handle, inode, lb);
  97. lb->first_pblock = lb->last_pblock = pblock;
  98. lb->first_block = lb->last_block = blk_num;
  99. return retval;
  100. }
  101. static int update_ind_extent_range(handle_t *handle, struct inode *inode,
  102. ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
  103. struct list_blocks_struct *lb)
  104. {
  105. struct buffer_head *bh;
  106. __le32 *i_data;
  107. int i, retval = 0;
  108. ext4_lblk_t blk_count = *blk_nump;
  109. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  110. if (!pblock) {
  111. /* Only update the file block number */
  112. *blk_nump += max_entries;
  113. return 0;
  114. }
  115. bh = sb_bread(inode->i_sb, pblock);
  116. if (!bh)
  117. return -EIO;
  118. i_data = (__le32 *)bh->b_data;
  119. for (i = 0; i < max_entries; i++, blk_count++) {
  120. if (i_data[i]) {
  121. retval = update_extent_range(handle, inode,
  122. le32_to_cpu(i_data[i]),
  123. blk_count, lb);
  124. if (retval)
  125. break;
  126. }
  127. }
  128. /* Update the file block number */
  129. *blk_nump = blk_count;
  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, ext4_lblk_t *blk_nump,
  135. struct list_blocks_struct *lb)
  136. {
  137. struct buffer_head *bh;
  138. __le32 *i_data;
  139. int i, retval = 0;
  140. ext4_lblk_t blk_count = *blk_nump;
  141. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  142. if (!pblock) {
  143. /* Only update the file block number */
  144. *blk_nump += max_entries * max_entries;
  145. return 0;
  146. }
  147. bh = sb_bread(inode->i_sb, pblock);
  148. if (!bh)
  149. return -EIO;
  150. i_data = (__le32 *)bh->b_data;
  151. for (i = 0; i < max_entries; i++) {
  152. if (i_data[i]) {
  153. retval = update_ind_extent_range(handle, inode,
  154. le32_to_cpu(i_data[i]),
  155. &blk_count, lb);
  156. if (retval)
  157. break;
  158. } else {
  159. /* Only update the file block number */
  160. blk_count += max_entries;
  161. }
  162. }
  163. /* Update the file block number */
  164. *blk_nump = blk_count;
  165. put_bh(bh);
  166. return retval;
  167. }
  168. static int update_tind_extent_range(handle_t *handle, struct inode *inode,
  169. ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
  170. struct list_blocks_struct *lb)
  171. {
  172. struct buffer_head *bh;
  173. __le32 *i_data;
  174. int i, retval = 0;
  175. ext4_lblk_t blk_count = *blk_nump;
  176. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  177. if (!pblock) {
  178. /* Only update the file block number */
  179. *blk_nump += max_entries * max_entries * max_entries;
  180. return 0;
  181. }
  182. bh = sb_bread(inode->i_sb, pblock);
  183. if (!bh)
  184. return -EIO;
  185. i_data = (__le32 *)bh->b_data;
  186. for (i = 0; i < max_entries; i++) {
  187. if (i_data[i]) {
  188. retval = update_dind_extent_range(handle, inode,
  189. le32_to_cpu(i_data[i]),
  190. &blk_count, lb);
  191. if (retval)
  192. break;
  193. } else
  194. /* Only update the file block number */
  195. blk_count += max_entries * max_entries;
  196. }
  197. /* Update the file block number */
  198. *blk_nump = blk_count;
  199. put_bh(bh);
  200. return retval;
  201. }
  202. static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode)
  203. {
  204. int retval = 0, needed;
  205. if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS)
  206. return 0;
  207. /*
  208. * We are freeing a blocks. During this we touch
  209. * superblock, group descriptor and block bitmap.
  210. * So allocate a credit of 3. We may update
  211. * quota (user and group).
  212. */
  213. needed = 3 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  214. if (ext4_journal_extend(handle, needed) != 0)
  215. retval = ext4_journal_restart(handle, needed);
  216. return retval;
  217. }
  218. static int free_dind_blocks(handle_t *handle,
  219. struct inode *inode, __le32 i_data)
  220. {
  221. int i;
  222. __le32 *tmp_idata;
  223. struct buffer_head *bh;
  224. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  225. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  226. if (!bh)
  227. return -EIO;
  228. tmp_idata = (__le32 *)bh->b_data;
  229. for (i = 0; i < max_entries; i++) {
  230. if (tmp_idata[i]) {
  231. extend_credit_for_blkdel(handle, inode);
  232. ext4_free_blocks(handle, inode,
  233. le32_to_cpu(tmp_idata[i]), 1, 1);
  234. }
  235. }
  236. put_bh(bh);
  237. extend_credit_for_blkdel(handle, inode);
  238. ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
  239. return 0;
  240. }
  241. static int free_tind_blocks(handle_t *handle,
  242. struct inode *inode, __le32 i_data)
  243. {
  244. int i, retval = 0;
  245. __le32 *tmp_idata;
  246. struct buffer_head *bh;
  247. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  248. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  249. if (!bh)
  250. return -EIO;
  251. tmp_idata = (__le32 *)bh->b_data;
  252. for (i = 0; i < max_entries; i++) {
  253. if (tmp_idata[i]) {
  254. retval = free_dind_blocks(handle,
  255. inode, tmp_idata[i]);
  256. if (retval) {
  257. put_bh(bh);
  258. return retval;
  259. }
  260. }
  261. }
  262. put_bh(bh);
  263. extend_credit_for_blkdel(handle, inode);
  264. ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
  265. return 0;
  266. }
  267. static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
  268. {
  269. int retval;
  270. /* ei->i_data[EXT4_IND_BLOCK] */
  271. if (i_data[0]) {
  272. extend_credit_for_blkdel(handle, inode);
  273. ext4_free_blocks(handle, inode,
  274. le32_to_cpu(i_data[0]), 1, 1);
  275. }
  276. /* ei->i_data[EXT4_DIND_BLOCK] */
  277. if (i_data[1]) {
  278. retval = free_dind_blocks(handle, inode, i_data[1]);
  279. if (retval)
  280. return retval;
  281. }
  282. /* ei->i_data[EXT4_TIND_BLOCK] */
  283. if (i_data[2]) {
  284. retval = free_tind_blocks(handle, inode, i_data[2]);
  285. if (retval)
  286. return retval;
  287. }
  288. return 0;
  289. }
  290. static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
  291. struct inode *tmp_inode)
  292. {
  293. int retval;
  294. __le32 i_data[3];
  295. struct ext4_inode_info *ei = EXT4_I(inode);
  296. struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
  297. /*
  298. * One credit accounted for writing the
  299. * i_data field of the original inode
  300. */
  301. retval = ext4_journal_extend(handle, 1);
  302. if (retval) {
  303. retval = ext4_journal_restart(handle, 1);
  304. if (retval)
  305. goto err_out;
  306. }
  307. i_data[0] = ei->i_data[EXT4_IND_BLOCK];
  308. i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
  309. i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
  310. down_write(&EXT4_I(inode)->i_data_sem);
  311. /*
  312. * if EXT4_EXT_MIGRATE is cleared a block allocation
  313. * happened after we started the migrate. We need to
  314. * fail the migrate
  315. */
  316. if (!(EXT4_I(inode)->i_flags & EXT4_EXT_MIGRATE)) {
  317. retval = -EAGAIN;
  318. up_write(&EXT4_I(inode)->i_data_sem);
  319. goto err_out;
  320. } else
  321. EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
  322. ~EXT4_EXT_MIGRATE;
  323. /*
  324. * We have the extent map build with the tmp inode.
  325. * Now copy the i_data across
  326. */
  327. ei->i_flags |= EXT4_EXTENTS_FL;
  328. memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
  329. /*
  330. * Update i_blocks with the new blocks that got
  331. * allocated while adding extents for extent index
  332. * blocks.
  333. *
  334. * While converting to extents we need not
  335. * update the orignal inode i_blocks for extent blocks
  336. * via quota APIs. The quota update happened via tmp_inode already.
  337. */
  338. spin_lock(&inode->i_lock);
  339. inode->i_blocks += tmp_inode->i_blocks;
  340. spin_unlock(&inode->i_lock);
  341. up_write(&EXT4_I(inode)->i_data_sem);
  342. /*
  343. * We mark the inode dirty after, because we decrement the
  344. * i_blocks when freeing the indirect meta-data blocks
  345. */
  346. retval = free_ind_block(handle, inode, i_data);
  347. ext4_mark_inode_dirty(handle, inode);
  348. err_out:
  349. return retval;
  350. }
  351. static int free_ext_idx(handle_t *handle, struct inode *inode,
  352. struct ext4_extent_idx *ix)
  353. {
  354. int i, retval = 0;
  355. ext4_fsblk_t block;
  356. struct buffer_head *bh;
  357. struct ext4_extent_header *eh;
  358. block = idx_pblock(ix);
  359. bh = sb_bread(inode->i_sb, block);
  360. if (!bh)
  361. return -EIO;
  362. eh = (struct ext4_extent_header *)bh->b_data;
  363. if (eh->eh_depth != 0) {
  364. ix = EXT_FIRST_INDEX(eh);
  365. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  366. retval = free_ext_idx(handle, inode, ix);
  367. if (retval)
  368. break;
  369. }
  370. }
  371. put_bh(bh);
  372. extend_credit_for_blkdel(handle, inode);
  373. ext4_free_blocks(handle, inode, block, 1, 1);
  374. return retval;
  375. }
  376. /*
  377. * Free the extent meta data blocks only
  378. */
  379. static int free_ext_block(handle_t *handle, struct inode *inode)
  380. {
  381. int i, retval = 0;
  382. struct ext4_inode_info *ei = EXT4_I(inode);
  383. struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
  384. struct ext4_extent_idx *ix;
  385. if (eh->eh_depth == 0)
  386. /*
  387. * No extra blocks allocated for extent meta data
  388. */
  389. return 0;
  390. ix = EXT_FIRST_INDEX(eh);
  391. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  392. retval = free_ext_idx(handle, inode, ix);
  393. if (retval)
  394. return retval;
  395. }
  396. return retval;
  397. }
  398. int ext4_ext_migrate(struct inode *inode)
  399. {
  400. handle_t *handle;
  401. int retval = 0, i;
  402. __le32 *i_data;
  403. ext4_lblk_t blk_count = 0;
  404. struct ext4_inode_info *ei;
  405. struct inode *tmp_inode = NULL;
  406. struct list_blocks_struct lb;
  407. unsigned long max_entries;
  408. if (!test_opt(inode->i_sb, EXTENTS))
  409. /*
  410. * if mounted with noextents we don't allow the migrate
  411. */
  412. return -EINVAL;
  413. if ((EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  414. return -EINVAL;
  415. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  416. /*
  417. * don't migrate fast symlink
  418. */
  419. return retval;
  420. handle = ext4_journal_start(inode,
  421. EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
  422. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  423. 2 * EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)
  424. + 1);
  425. if (IS_ERR(handle)) {
  426. retval = PTR_ERR(handle);
  427. goto err_out;
  428. }
  429. tmp_inode = ext4_new_inode(handle,
  430. inode->i_sb->s_root->d_inode,
  431. S_IFREG);
  432. if (IS_ERR(tmp_inode)) {
  433. retval = -ENOMEM;
  434. ext4_journal_stop(handle);
  435. tmp_inode = NULL;
  436. goto err_out;
  437. }
  438. i_size_write(tmp_inode, i_size_read(inode));
  439. /*
  440. * We don't want the inode to be reclaimed
  441. * if we got interrupted in between. We have
  442. * this tmp inode carrying reference to the
  443. * data blocks of the original file. We set
  444. * the i_nlink to zero at the last stage after
  445. * switching the original file to extent format
  446. */
  447. tmp_inode->i_nlink = 1;
  448. ext4_ext_tree_init(handle, tmp_inode);
  449. ext4_orphan_add(handle, tmp_inode);
  450. ext4_journal_stop(handle);
  451. /*
  452. * start with one credit accounted for
  453. * superblock modification.
  454. *
  455. * For the tmp_inode we already have commited the
  456. * trascation that created the inode. Later as and
  457. * when we add extents we extent the journal
  458. */
  459. /*
  460. * Even though we take i_mutex we can still cause block allocation
  461. * via mmap write to holes. If we have allocated new blocks we fail
  462. * migrate. New block allocation will clear EXT4_EXT_MIGRATE flag.
  463. * The flag is updated with i_data_sem held to prevent racing with
  464. * block allocation.
  465. */
  466. down_read((&EXT4_I(inode)->i_data_sem));
  467. EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags | EXT4_EXT_MIGRATE;
  468. up_read((&EXT4_I(inode)->i_data_sem));
  469. handle = ext4_journal_start(inode, 1);
  470. ei = EXT4_I(inode);
  471. i_data = ei->i_data;
  472. memset(&lb, 0, sizeof(lb));
  473. /* 32 bit block address 4 bytes */
  474. max_entries = inode->i_sb->s_blocksize >> 2;
  475. for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) {
  476. if (i_data[i]) {
  477. retval = update_extent_range(handle, tmp_inode,
  478. le32_to_cpu(i_data[i]),
  479. blk_count, &lb);
  480. if (retval)
  481. goto err_out;
  482. }
  483. }
  484. if (i_data[EXT4_IND_BLOCK]) {
  485. retval = update_ind_extent_range(handle, tmp_inode,
  486. le32_to_cpu(i_data[EXT4_IND_BLOCK]),
  487. &blk_count, &lb);
  488. if (retval)
  489. goto err_out;
  490. } else
  491. blk_count += max_entries;
  492. if (i_data[EXT4_DIND_BLOCK]) {
  493. retval = update_dind_extent_range(handle, tmp_inode,
  494. le32_to_cpu(i_data[EXT4_DIND_BLOCK]),
  495. &blk_count, &lb);
  496. if (retval)
  497. goto err_out;
  498. } else
  499. blk_count += max_entries * max_entries;
  500. if (i_data[EXT4_TIND_BLOCK]) {
  501. retval = update_tind_extent_range(handle, tmp_inode,
  502. le32_to_cpu(i_data[EXT4_TIND_BLOCK]),
  503. &blk_count, &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_delete_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. /*
  546. * Set the i_nlink to zero so that
  547. * generic_drop_inode really deletes the
  548. * inode
  549. */
  550. tmp_inode->i_nlink = 0;
  551. ext4_journal_stop(handle);
  552. if (tmp_inode)
  553. iput(tmp_inode);
  554. return retval;
  555. }