migrate.c 15 KB

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