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/module.h>
  15. #include <linux/slab.h>
  16. #include "ext4_jbd2.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. 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 && ext4_handle_has_enough_credits(handle,
  55. EXT4_RESERVE_TRANS_BLOCKS)) {
  56. retval = ext4_journal_restart(handle, needed);
  57. if (retval)
  58. goto err_out;
  59. } else if (needed) {
  60. retval = ext4_journal_extend(handle, needed);
  61. if (retval) {
  62. /*
  63. * IF not able to extend the journal restart the journal
  64. */
  65. retval = ext4_journal_restart(handle, needed);
  66. if (retval)
  67. goto err_out;
  68. }
  69. }
  70. retval = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
  71. err_out:
  72. if (path) {
  73. ext4_ext_drop_refs(path);
  74. kfree(path);
  75. }
  76. lb->first_pblock = 0;
  77. return retval;
  78. }
  79. static int update_extent_range(handle_t *handle, struct inode *inode,
  80. ext4_fsblk_t pblock, struct migrate_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 == lb->curr_block)) {
  89. lb->last_pblock = pblock;
  90. lb->last_block = lb->curr_block;
  91. lb->curr_block++;
  92. return 0;
  93. }
  94. /*
  95. * Start a new range.
  96. */
  97. retval = finish_range(handle, inode, lb);
  98. lb->first_pblock = lb->last_pblock = pblock;
  99. lb->first_block = lb->last_block = lb->curr_block;
  100. lb->curr_block++;
  101. return retval;
  102. }
  103. static int update_ind_extent_range(handle_t *handle, struct inode *inode,
  104. ext4_fsblk_t pblock,
  105. struct migrate_struct *lb)
  106. {
  107. struct buffer_head *bh;
  108. __le32 *i_data;
  109. int i, retval = 0;
  110. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  111. bh = sb_bread(inode->i_sb, pblock);
  112. if (!bh)
  113. return -EIO;
  114. i_data = (__le32 *)bh->b_data;
  115. for (i = 0; i < max_entries; i++) {
  116. if (i_data[i]) {
  117. retval = update_extent_range(handle, inode,
  118. le32_to_cpu(i_data[i]), lb);
  119. if (retval)
  120. break;
  121. } else {
  122. lb->curr_block++;
  123. }
  124. }
  125. put_bh(bh);
  126. return retval;
  127. }
  128. static int update_dind_extent_range(handle_t *handle, struct inode *inode,
  129. ext4_fsblk_t pblock,
  130. struct migrate_struct *lb)
  131. {
  132. struct buffer_head *bh;
  133. __le32 *i_data;
  134. int i, retval = 0;
  135. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  136. bh = sb_bread(inode->i_sb, pblock);
  137. if (!bh)
  138. return -EIO;
  139. i_data = (__le32 *)bh->b_data;
  140. for (i = 0; i < max_entries; i++) {
  141. if (i_data[i]) {
  142. retval = update_ind_extent_range(handle, inode,
  143. le32_to_cpu(i_data[i]), lb);
  144. if (retval)
  145. break;
  146. } else {
  147. /* Only update the file block number */
  148. lb->curr_block += max_entries;
  149. }
  150. }
  151. put_bh(bh);
  152. return retval;
  153. }
  154. static int update_tind_extent_range(handle_t *handle, struct inode *inode,
  155. ext4_fsblk_t pblock,
  156. struct migrate_struct *lb)
  157. {
  158. struct buffer_head *bh;
  159. __le32 *i_data;
  160. int i, retval = 0;
  161. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  162. bh = sb_bread(inode->i_sb, pblock);
  163. if (!bh)
  164. return -EIO;
  165. i_data = (__le32 *)bh->b_data;
  166. for (i = 0; i < max_entries; i++) {
  167. if (i_data[i]) {
  168. retval = update_dind_extent_range(handle, inode,
  169. le32_to_cpu(i_data[i]), lb);
  170. if (retval)
  171. break;
  172. } else {
  173. /* Only update the file block number */
  174. lb->curr_block += max_entries * max_entries;
  175. }
  176. }
  177. put_bh(bh);
  178. return retval;
  179. }
  180. static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode)
  181. {
  182. int retval = 0, needed;
  183. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  184. return 0;
  185. /*
  186. * We are freeing a blocks. During this we touch
  187. * superblock, group descriptor and block bitmap.
  188. * So allocate a credit of 3. We may update
  189. * quota (user and group).
  190. */
  191. needed = 3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  192. if (ext4_journal_extend(handle, needed) != 0)
  193. retval = ext4_journal_restart(handle, needed);
  194. return retval;
  195. }
  196. static int free_dind_blocks(handle_t *handle,
  197. struct inode *inode, __le32 i_data)
  198. {
  199. int i;
  200. __le32 *tmp_idata;
  201. struct buffer_head *bh;
  202. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  203. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  204. if (!bh)
  205. return -EIO;
  206. tmp_idata = (__le32 *)bh->b_data;
  207. for (i = 0; i < max_entries; i++) {
  208. if (tmp_idata[i]) {
  209. extend_credit_for_blkdel(handle, inode);
  210. ext4_free_blocks(handle, inode, NULL,
  211. le32_to_cpu(tmp_idata[i]), 1,
  212. EXT4_FREE_BLOCKS_METADATA |
  213. EXT4_FREE_BLOCKS_FORGET);
  214. }
  215. }
  216. put_bh(bh);
  217. extend_credit_for_blkdel(handle, inode);
  218. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  219. EXT4_FREE_BLOCKS_METADATA |
  220. EXT4_FREE_BLOCKS_FORGET);
  221. return 0;
  222. }
  223. static int free_tind_blocks(handle_t *handle,
  224. struct inode *inode, __le32 i_data)
  225. {
  226. int i, retval = 0;
  227. __le32 *tmp_idata;
  228. struct buffer_head *bh;
  229. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  230. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  231. if (!bh)
  232. return -EIO;
  233. tmp_idata = (__le32 *)bh->b_data;
  234. for (i = 0; i < max_entries; i++) {
  235. if (tmp_idata[i]) {
  236. retval = free_dind_blocks(handle,
  237. inode, tmp_idata[i]);
  238. if (retval) {
  239. put_bh(bh);
  240. return retval;
  241. }
  242. }
  243. }
  244. put_bh(bh);
  245. extend_credit_for_blkdel(handle, inode);
  246. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1,
  247. EXT4_FREE_BLOCKS_METADATA |
  248. EXT4_FREE_BLOCKS_FORGET);
  249. return 0;
  250. }
  251. static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
  252. {
  253. int retval;
  254. /* ei->i_data[EXT4_IND_BLOCK] */
  255. if (i_data[0]) {
  256. extend_credit_for_blkdel(handle, inode);
  257. ext4_free_blocks(handle, inode, NULL,
  258. le32_to_cpu(i_data[0]), 1,
  259. EXT4_FREE_BLOCKS_METADATA |
  260. EXT4_FREE_BLOCKS_FORGET);
  261. }
  262. /* ei->i_data[EXT4_DIND_BLOCK] */
  263. if (i_data[1]) {
  264. retval = free_dind_blocks(handle, inode, i_data[1]);
  265. if (retval)
  266. return retval;
  267. }
  268. /* ei->i_data[EXT4_TIND_BLOCK] */
  269. if (i_data[2]) {
  270. retval = free_tind_blocks(handle, inode, i_data[2]);
  271. if (retval)
  272. return retval;
  273. }
  274. return 0;
  275. }
  276. static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
  277. struct inode *tmp_inode)
  278. {
  279. int retval;
  280. __le32 i_data[3];
  281. struct ext4_inode_info *ei = EXT4_I(inode);
  282. struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
  283. /*
  284. * One credit accounted for writing the
  285. * i_data field of the original inode
  286. */
  287. retval = ext4_journal_extend(handle, 1);
  288. if (retval) {
  289. retval = ext4_journal_restart(handle, 1);
  290. if (retval)
  291. goto err_out;
  292. }
  293. i_data[0] = ei->i_data[EXT4_IND_BLOCK];
  294. i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
  295. i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
  296. down_write(&EXT4_I(inode)->i_data_sem);
  297. /*
  298. * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
  299. * happened after we started the migrate. We need to
  300. * fail the migrate
  301. */
  302. if (!ext4_test_inode_state(inode, EXT4_STATE_EXT_MIGRATE)) {
  303. retval = -EAGAIN;
  304. up_write(&EXT4_I(inode)->i_data_sem);
  305. goto err_out;
  306. } else
  307. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  308. /*
  309. * We have the extent map build with the tmp inode.
  310. * Now copy the i_data across
  311. */
  312. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  313. memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
  314. /*
  315. * Update i_blocks with the new blocks that got
  316. * allocated while adding extents for extent index
  317. * blocks.
  318. *
  319. * While converting to extents we need not
  320. * update the orignal inode i_blocks for extent blocks
  321. * via quota APIs. The quota update happened via tmp_inode already.
  322. */
  323. spin_lock(&inode->i_lock);
  324. inode->i_blocks += tmp_inode->i_blocks;
  325. spin_unlock(&inode->i_lock);
  326. up_write(&EXT4_I(inode)->i_data_sem);
  327. /*
  328. * We mark the inode dirty after, because we decrement the
  329. * i_blocks when freeing the indirect meta-data blocks
  330. */
  331. retval = free_ind_block(handle, inode, i_data);
  332. ext4_mark_inode_dirty(handle, inode);
  333. err_out:
  334. return retval;
  335. }
  336. static int free_ext_idx(handle_t *handle, struct inode *inode,
  337. struct ext4_extent_idx *ix)
  338. {
  339. int i, retval = 0;
  340. ext4_fsblk_t block;
  341. struct buffer_head *bh;
  342. struct ext4_extent_header *eh;
  343. block = ext4_idx_pblock(ix);
  344. bh = sb_bread(inode->i_sb, block);
  345. if (!bh)
  346. return -EIO;
  347. eh = (struct ext4_extent_header *)bh->b_data;
  348. if (eh->eh_depth != 0) {
  349. ix = EXT_FIRST_INDEX(eh);
  350. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  351. retval = free_ext_idx(handle, inode, ix);
  352. if (retval)
  353. break;
  354. }
  355. }
  356. put_bh(bh);
  357. extend_credit_for_blkdel(handle, inode);
  358. ext4_free_blocks(handle, inode, NULL, block, 1,
  359. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  360. return retval;
  361. }
  362. /*
  363. * Free the extent meta data blocks only
  364. */
  365. static int free_ext_block(handle_t *handle, struct inode *inode)
  366. {
  367. int i, retval = 0;
  368. struct ext4_inode_info *ei = EXT4_I(inode);
  369. struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
  370. struct ext4_extent_idx *ix;
  371. if (eh->eh_depth == 0)
  372. /*
  373. * No extra blocks allocated for extent meta data
  374. */
  375. return 0;
  376. ix = EXT_FIRST_INDEX(eh);
  377. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  378. retval = free_ext_idx(handle, inode, ix);
  379. if (retval)
  380. return retval;
  381. }
  382. return retval;
  383. }
  384. int ext4_ext_migrate(struct inode *inode)
  385. {
  386. handle_t *handle;
  387. int retval = 0, i;
  388. __le32 *i_data;
  389. struct ext4_inode_info *ei;
  390. struct inode *tmp_inode = NULL;
  391. struct migrate_struct lb;
  392. unsigned long max_entries;
  393. __u32 goal;
  394. uid_t owner[2];
  395. /*
  396. * If the filesystem does not support extents, or the inode
  397. * already is extent-based, error out.
  398. */
  399. if (!EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
  400. EXT4_FEATURE_INCOMPAT_EXTENTS) ||
  401. (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  402. return -EINVAL;
  403. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  404. /*
  405. * don't migrate fast symlink
  406. */
  407. return retval;
  408. handle = ext4_journal_start(inode,
  409. EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
  410. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  411. EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)
  412. + 1);
  413. if (IS_ERR(handle)) {
  414. retval = PTR_ERR(handle);
  415. return retval;
  416. }
  417. goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
  418. EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
  419. owner[0] = inode->i_uid;
  420. owner[1] = inode->i_gid;
  421. tmp_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  422. S_IFREG, NULL, goal, owner);
  423. if (IS_ERR(tmp_inode)) {
  424. retval = PTR_ERR(inode);
  425. ext4_journal_stop(handle);
  426. return retval;
  427. }
  428. i_size_write(tmp_inode, i_size_read(inode));
  429. /*
  430. * Set the i_nlink to zero so it will be deleted later
  431. * when we drop inode reference.
  432. */
  433. clear_nlink(tmp_inode);
  434. ext4_ext_tree_init(handle, tmp_inode);
  435. ext4_orphan_add(handle, tmp_inode);
  436. ext4_journal_stop(handle);
  437. /*
  438. * start with one credit accounted for
  439. * superblock modification.
  440. *
  441. * For the tmp_inode we already have committed the
  442. * trascation that created the inode. Later as and
  443. * when we add extents we extent the journal
  444. */
  445. /*
  446. * Even though we take i_mutex we can still cause block
  447. * allocation via mmap write to holes. If we have allocated
  448. * new blocks we fail migrate. New block allocation will
  449. * clear EXT4_STATE_EXT_MIGRATE flag. The flag is updated
  450. * with i_data_sem held to prevent racing with block
  451. * allocation.
  452. */
  453. down_read((&EXT4_I(inode)->i_data_sem));
  454. ext4_set_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  455. up_read((&EXT4_I(inode)->i_data_sem));
  456. handle = ext4_journal_start(inode, 1);
  457. if (IS_ERR(handle)) {
  458. /*
  459. * It is impossible to update on-disk structures without
  460. * a handle, so just rollback in-core changes and live other
  461. * work to orphan_list_cleanup()
  462. */
  463. ext4_orphan_del(NULL, tmp_inode);
  464. retval = PTR_ERR(handle);
  465. goto out;
  466. }
  467. ei = EXT4_I(inode);
  468. i_data = ei->i_data;
  469. memset(&lb, 0, sizeof(lb));
  470. /* 32 bit block address 4 bytes */
  471. max_entries = inode->i_sb->s_blocksize >> 2;
  472. for (i = 0; i < EXT4_NDIR_BLOCKS; i++) {
  473. if (i_data[i]) {
  474. retval = update_extent_range(handle, tmp_inode,
  475. le32_to_cpu(i_data[i]), &lb);
  476. if (retval)
  477. goto err_out;
  478. } else
  479. lb.curr_block++;
  480. }
  481. if (i_data[EXT4_IND_BLOCK]) {
  482. retval = update_ind_extent_range(handle, tmp_inode,
  483. le32_to_cpu(i_data[EXT4_IND_BLOCK]), &lb);
  484. if (retval)
  485. goto err_out;
  486. } else
  487. lb.curr_block += max_entries;
  488. if (i_data[EXT4_DIND_BLOCK]) {
  489. retval = update_dind_extent_range(handle, tmp_inode,
  490. le32_to_cpu(i_data[EXT4_DIND_BLOCK]), &lb);
  491. if (retval)
  492. goto err_out;
  493. } else
  494. lb.curr_block += max_entries * max_entries;
  495. if (i_data[EXT4_TIND_BLOCK]) {
  496. retval = update_tind_extent_range(handle, tmp_inode,
  497. le32_to_cpu(i_data[EXT4_TIND_BLOCK]), &lb);
  498. if (retval)
  499. goto err_out;
  500. }
  501. /*
  502. * Build the last extent
  503. */
  504. retval = finish_range(handle, tmp_inode, &lb);
  505. err_out:
  506. if (retval)
  507. /*
  508. * Failure case delete the extent information with the
  509. * tmp_inode
  510. */
  511. free_ext_block(handle, tmp_inode);
  512. else {
  513. retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
  514. if (retval)
  515. /*
  516. * if we fail to swap inode data free the extent
  517. * details of the tmp inode
  518. */
  519. free_ext_block(handle, tmp_inode);
  520. }
  521. /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
  522. if (ext4_journal_extend(handle, 1) != 0)
  523. ext4_journal_restart(handle, 1);
  524. /*
  525. * Mark the tmp_inode as of size zero
  526. */
  527. i_size_write(tmp_inode, 0);
  528. /*
  529. * set the i_blocks count to zero
  530. * so that the ext4_delete_inode does the
  531. * right job
  532. *
  533. * We don't need to take the i_lock because
  534. * the inode is not visible to user space.
  535. */
  536. tmp_inode->i_blocks = 0;
  537. /* Reset the extent details */
  538. ext4_ext_tree_init(handle, tmp_inode);
  539. ext4_journal_stop(handle);
  540. out:
  541. unlock_new_inode(tmp_inode);
  542. iput(tmp_inode);
  543. return retval;
  544. }