migrate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. /*
  17. * The contiguous blocks details which can be
  18. * represented by a single extent
  19. */
  20. struct migrate_struct {
  21. ext4_lblk_t first_block, last_block, curr_block;
  22. ext4_fsblk_t first_pblock, last_pblock;
  23. };
  24. static int finish_range(handle_t *handle, struct inode *inode,
  25. struct migrate_struct *lb)
  26. {
  27. int retval = 0, needed;
  28. struct ext4_extent newext;
  29. struct ext4_ext_path *path;
  30. if (lb->first_pblock == 0)
  31. return 0;
  32. /* Add the extent to temp inode*/
  33. newext.ee_block = cpu_to_le32(lb->first_block);
  34. newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block + 1);
  35. ext4_ext_store_pblock(&newext, lb->first_pblock);
  36. path = ext4_ext_find_extent(inode, lb->first_block, NULL);
  37. if (IS_ERR(path)) {
  38. retval = PTR_ERR(path);
  39. path = NULL;
  40. goto err_out;
  41. }
  42. /*
  43. * Calculate the credit needed to inserting this extent
  44. * Since we are doing this in loop we may accumalate extra
  45. * credit. But below we try to not accumalate too much
  46. * of them by restarting the journal.
  47. */
  48. needed = ext4_ext_calc_credits_for_single_extent(inode,
  49. lb->last_block - lb->first_block + 1, path);
  50. /*
  51. * Make sure the credit we accumalated is not really high
  52. */
  53. if (needed && ext4_handle_has_enough_credits(handle,
  54. 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, 0);
  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, struct migrate_struct *lb)
  80. {
  81. int retval;
  82. /*
  83. * See if we can add on to the existing range (if it exists)
  84. */
  85. if (lb->first_pblock &&
  86. (lb->last_pblock+1 == pblock) &&
  87. (lb->last_block+1 == lb->curr_block)) {
  88. lb->last_pblock = pblock;
  89. lb->last_block = lb->curr_block;
  90. lb->curr_block++;
  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 = lb->curr_block;
  99. lb->curr_block++;
  100. return retval;
  101. }
  102. static int update_ind_extent_range(handle_t *handle, struct inode *inode,
  103. ext4_fsblk_t pblock,
  104. struct migrate_struct *lb)
  105. {
  106. struct buffer_head *bh;
  107. __le32 *i_data;
  108. int i, retval = 0;
  109. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  110. bh = sb_bread(inode->i_sb, pblock);
  111. if (!bh)
  112. return -EIO;
  113. i_data = (__le32 *)bh->b_data;
  114. for (i = 0; i < max_entries; i++) {
  115. if (i_data[i]) {
  116. retval = update_extent_range(handle, inode,
  117. le32_to_cpu(i_data[i]), lb);
  118. if (retval)
  119. break;
  120. } else {
  121. lb->curr_block++;
  122. }
  123. }
  124. put_bh(bh);
  125. return retval;
  126. }
  127. static int update_dind_extent_range(handle_t *handle, struct inode *inode,
  128. ext4_fsblk_t pblock,
  129. struct migrate_struct *lb)
  130. {
  131. struct buffer_head *bh;
  132. __le32 *i_data;
  133. int i, retval = 0;
  134. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  135. bh = sb_bread(inode->i_sb, pblock);
  136. if (!bh)
  137. return -EIO;
  138. i_data = (__le32 *)bh->b_data;
  139. for (i = 0; i < max_entries; i++) {
  140. if (i_data[i]) {
  141. retval = update_ind_extent_range(handle, inode,
  142. le32_to_cpu(i_data[i]), lb);
  143. if (retval)
  144. break;
  145. } else {
  146. /* Only update the file block number */
  147. lb->curr_block += max_entries;
  148. }
  149. }
  150. put_bh(bh);
  151. return retval;
  152. }
  153. static int update_tind_extent_range(handle_t *handle, struct inode *inode,
  154. ext4_fsblk_t pblock,
  155. struct migrate_struct *lb)
  156. {
  157. struct buffer_head *bh;
  158. __le32 *i_data;
  159. int i, retval = 0;
  160. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  161. bh = sb_bread(inode->i_sb, pblock);
  162. if (!bh)
  163. return -EIO;
  164. i_data = (__le32 *)bh->b_data;
  165. for (i = 0; i < max_entries; i++) {
  166. if (i_data[i]) {
  167. retval = update_dind_extent_range(handle, inode,
  168. le32_to_cpu(i_data[i]), lb);
  169. if (retval)
  170. break;
  171. } else {
  172. /* Only update the file block number */
  173. lb->curr_block += max_entries * max_entries;
  174. }
  175. }
  176. put_bh(bh);
  177. return retval;
  178. }
  179. static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode)
  180. {
  181. int retval = 0, needed;
  182. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  183. return 0;
  184. /*
  185. * We are freeing a blocks. During this we touch
  186. * superblock, group descriptor and block bitmap.
  187. * So allocate a credit of 3. We may update
  188. * quota (user and group).
  189. */
  190. needed = 3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  191. if (ext4_journal_extend(handle, needed) != 0)
  192. retval = ext4_journal_restart(handle, needed);
  193. return retval;
  194. }
  195. static int free_dind_blocks(handle_t *handle,
  196. struct inode *inode, __le32 i_data)
  197. {
  198. int i;
  199. __le32 *tmp_idata;
  200. struct buffer_head *bh;
  201. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  202. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  203. if (!bh)
  204. return -EIO;
  205. tmp_idata = (__le32 *)bh->b_data;
  206. for (i = 0; i < max_entries; i++) {
  207. if (tmp_idata[i]) {
  208. extend_credit_for_blkdel(handle, inode);
  209. ext4_free_blocks(handle, inode, NULL,
  210. le32_to_cpu(tmp_idata[i]), 1,
  211. EXT4_FREE_BLOCKS_METADATA |
  212. EXT4_FREE_BLOCKS_FORGET);
  213. }
  214. }
  215. put_bh(bh);
  216. extend_credit_for_blkdel(handle, inode);
  217. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  218. EXT4_FREE_BLOCKS_METADATA |
  219. EXT4_FREE_BLOCKS_FORGET);
  220. return 0;
  221. }
  222. static int free_tind_blocks(handle_t *handle,
  223. struct inode *inode, __le32 i_data)
  224. {
  225. int i, retval = 0;
  226. __le32 *tmp_idata;
  227. struct buffer_head *bh;
  228. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  229. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  230. if (!bh)
  231. return -EIO;
  232. tmp_idata = (__le32 *)bh->b_data;
  233. for (i = 0; i < max_entries; i++) {
  234. if (tmp_idata[i]) {
  235. retval = free_dind_blocks(handle,
  236. inode, tmp_idata[i]);
  237. if (retval) {
  238. put_bh(bh);
  239. return retval;
  240. }
  241. }
  242. }
  243. put_bh(bh);
  244. extend_credit_for_blkdel(handle, inode);
  245. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  246. EXT4_FREE_BLOCKS_METADATA |
  247. EXT4_FREE_BLOCKS_FORGET);
  248. return 0;
  249. }
  250. static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
  251. {
  252. int retval;
  253. /* ei->i_data[EXT4_IND_BLOCK] */
  254. if (i_data[0]) {
  255. extend_credit_for_blkdel(handle, inode);
  256. ext4_free_blocks(handle, inode, NULL,
  257. le32_to_cpu(i_data[0]), 1,
  258. EXT4_FREE_BLOCKS_METADATA |
  259. EXT4_FREE_BLOCKS_FORGET);
  260. }
  261. /* ei->i_data[EXT4_DIND_BLOCK] */
  262. if (i_data[1]) {
  263. retval = free_dind_blocks(handle, inode, i_data[1]);
  264. if (retval)
  265. return retval;
  266. }
  267. /* ei->i_data[EXT4_TIND_BLOCK] */
  268. if (i_data[2]) {
  269. retval = free_tind_blocks(handle, inode, i_data[2]);
  270. if (retval)
  271. return retval;
  272. }
  273. return 0;
  274. }
  275. static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
  276. struct inode *tmp_inode)
  277. {
  278. int retval;
  279. __le32 i_data[3];
  280. struct ext4_inode_info *ei = EXT4_I(inode);
  281. struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
  282. /*
  283. * One credit accounted for writing the
  284. * i_data field of the original inode
  285. */
  286. retval = ext4_journal_extend(handle, 1);
  287. if (retval) {
  288. retval = ext4_journal_restart(handle, 1);
  289. if (retval)
  290. goto err_out;
  291. }
  292. i_data[0] = ei->i_data[EXT4_IND_BLOCK];
  293. i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
  294. i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
  295. down_write(&EXT4_I(inode)->i_data_sem);
  296. /*
  297. * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
  298. * happened after we started the migrate. We need to
  299. * fail the migrate
  300. */
  301. if (!ext4_test_inode_state(inode, EXT4_STATE_EXT_MIGRATE)) {
  302. retval = -EAGAIN;
  303. up_write(&EXT4_I(inode)->i_data_sem);
  304. goto err_out;
  305. } else
  306. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  307. /*
  308. * We have the extent map build with the tmp inode.
  309. * Now copy the i_data across
  310. */
  311. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  312. memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
  313. /*
  314. * Update i_blocks with the new blocks that got
  315. * allocated while adding extents for extent index
  316. * blocks.
  317. *
  318. * While converting to extents we need not
  319. * update the orignal inode i_blocks for extent blocks
  320. * via quota APIs. The quota update happened via tmp_inode already.
  321. */
  322. spin_lock(&inode->i_lock);
  323. inode->i_blocks += tmp_inode->i_blocks;
  324. spin_unlock(&inode->i_lock);
  325. up_write(&EXT4_I(inode)->i_data_sem);
  326. /*
  327. * We mark the inode dirty after, because we decrement the
  328. * i_blocks when freeing the indirect meta-data blocks
  329. */
  330. retval = free_ind_block(handle, inode, i_data);
  331. ext4_mark_inode_dirty(handle, inode);
  332. err_out:
  333. return retval;
  334. }
  335. static int free_ext_idx(handle_t *handle, struct inode *inode,
  336. struct ext4_extent_idx *ix)
  337. {
  338. int i, retval = 0;
  339. ext4_fsblk_t block;
  340. struct buffer_head *bh;
  341. struct ext4_extent_header *eh;
  342. block = ext4_idx_pblock(ix);
  343. bh = sb_bread(inode->i_sb, block);
  344. if (!bh)
  345. return -EIO;
  346. eh = (struct ext4_extent_header *)bh->b_data;
  347. if (eh->eh_depth != 0) {
  348. ix = EXT_FIRST_INDEX(eh);
  349. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  350. retval = free_ext_idx(handle, inode, ix);
  351. if (retval)
  352. break;
  353. }
  354. }
  355. put_bh(bh);
  356. extend_credit_for_blkdel(handle, inode);
  357. ext4_free_blocks(handle, inode, NULL, block, 1,
  358. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  359. return retval;
  360. }
  361. /*
  362. * Free the extent meta data blocks only
  363. */
  364. static int free_ext_block(handle_t *handle, struct inode *inode)
  365. {
  366. int i, retval = 0;
  367. struct ext4_inode_info *ei = EXT4_I(inode);
  368. struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
  369. struct ext4_extent_idx *ix;
  370. if (eh->eh_depth == 0)
  371. /*
  372. * No extra blocks allocated for extent meta data
  373. */
  374. return 0;
  375. ix = EXT_FIRST_INDEX(eh);
  376. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  377. retval = free_ext_idx(handle, inode, ix);
  378. if (retval)
  379. return retval;
  380. }
  381. return retval;
  382. }
  383. int ext4_ext_migrate(struct inode *inode)
  384. {
  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_INCOMPAT_FEATURE(inode->i_sb,
  399. EXT4_FEATURE_INCOMPAT_EXTENTS) ||
  400. (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  401. return -EINVAL;
  402. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  403. /*
  404. * don't migrate fast symlink
  405. */
  406. return retval;
  407. handle = ext4_journal_start(inode,
  408. EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
  409. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  410. EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)
  411. + 1);
  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] = inode->i_uid;
  419. owner[1] = inode->i_gid;
  420. tmp_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  421. S_IFREG, NULL, goal, owner);
  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. * trascation 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, 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_delete_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. }