migrate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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, 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, 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 + EXT4_MAXQUOTAS_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, 0,
  234. le32_to_cpu(tmp_idata[i]), 1,
  235. EXT4_FREE_BLOCKS_METADATA |
  236. EXT4_FREE_BLOCKS_FORGET);
  237. }
  238. }
  239. put_bh(bh);
  240. extend_credit_for_blkdel(handle, inode);
  241. ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
  242. EXT4_FREE_BLOCKS_METADATA |
  243. EXT4_FREE_BLOCKS_FORGET);
  244. return 0;
  245. }
  246. static int free_tind_blocks(handle_t *handle,
  247. struct inode *inode, __le32 i_data)
  248. {
  249. int i, retval = 0;
  250. __le32 *tmp_idata;
  251. struct buffer_head *bh;
  252. unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
  253. bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
  254. if (!bh)
  255. return -EIO;
  256. tmp_idata = (__le32 *)bh->b_data;
  257. for (i = 0; i < max_entries; i++) {
  258. if (tmp_idata[i]) {
  259. retval = free_dind_blocks(handle,
  260. inode, tmp_idata[i]);
  261. if (retval) {
  262. put_bh(bh);
  263. return retval;
  264. }
  265. }
  266. }
  267. put_bh(bh);
  268. extend_credit_for_blkdel(handle, inode);
  269. ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
  270. EXT4_FREE_BLOCKS_METADATA |
  271. EXT4_FREE_BLOCKS_FORGET);
  272. return 0;
  273. }
  274. static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
  275. {
  276. int retval;
  277. /* ei->i_data[EXT4_IND_BLOCK] */
  278. if (i_data[0]) {
  279. extend_credit_for_blkdel(handle, inode);
  280. ext4_free_blocks(handle, inode, 0,
  281. le32_to_cpu(i_data[0]), 1,
  282. EXT4_FREE_BLOCKS_METADATA |
  283. EXT4_FREE_BLOCKS_FORGET);
  284. }
  285. /* ei->i_data[EXT4_DIND_BLOCK] */
  286. if (i_data[1]) {
  287. retval = free_dind_blocks(handle, inode, i_data[1]);
  288. if (retval)
  289. return retval;
  290. }
  291. /* ei->i_data[EXT4_TIND_BLOCK] */
  292. if (i_data[2]) {
  293. retval = free_tind_blocks(handle, inode, i_data[2]);
  294. if (retval)
  295. return retval;
  296. }
  297. return 0;
  298. }
  299. static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
  300. struct inode *tmp_inode)
  301. {
  302. int retval;
  303. __le32 i_data[3];
  304. struct ext4_inode_info *ei = EXT4_I(inode);
  305. struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
  306. /*
  307. * One credit accounted for writing the
  308. * i_data field of the original inode
  309. */
  310. retval = ext4_journal_extend(handle, 1);
  311. if (retval) {
  312. retval = ext4_journal_restart(handle, 1);
  313. if (retval)
  314. goto err_out;
  315. }
  316. i_data[0] = ei->i_data[EXT4_IND_BLOCK];
  317. i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
  318. i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
  319. down_write(&EXT4_I(inode)->i_data_sem);
  320. /*
  321. * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
  322. * happened after we started the migrate. We need to
  323. * fail the migrate
  324. */
  325. if (!(EXT4_I(inode)->i_state & EXT4_STATE_EXT_MIGRATE)) {
  326. retval = -EAGAIN;
  327. up_write(&EXT4_I(inode)->i_data_sem);
  328. goto err_out;
  329. } else
  330. EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
  331. /*
  332. * We have the extent map build with the tmp inode.
  333. * Now copy the i_data across
  334. */
  335. ei->i_flags |= EXT4_EXTENTS_FL;
  336. memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
  337. /*
  338. * Update i_blocks with the new blocks that got
  339. * allocated while adding extents for extent index
  340. * blocks.
  341. *
  342. * While converting to extents we need not
  343. * update the orignal inode i_blocks for extent blocks
  344. * via quota APIs. The quota update happened via tmp_inode already.
  345. */
  346. spin_lock(&inode->i_lock);
  347. inode->i_blocks += tmp_inode->i_blocks;
  348. spin_unlock(&inode->i_lock);
  349. up_write(&EXT4_I(inode)->i_data_sem);
  350. /*
  351. * We mark the inode dirty after, because we decrement the
  352. * i_blocks when freeing the indirect meta-data blocks
  353. */
  354. retval = free_ind_block(handle, inode, i_data);
  355. ext4_mark_inode_dirty(handle, inode);
  356. err_out:
  357. return retval;
  358. }
  359. static int free_ext_idx(handle_t *handle, struct inode *inode,
  360. struct ext4_extent_idx *ix)
  361. {
  362. int i, retval = 0;
  363. ext4_fsblk_t block;
  364. struct buffer_head *bh;
  365. struct ext4_extent_header *eh;
  366. block = idx_pblock(ix);
  367. bh = sb_bread(inode->i_sb, block);
  368. if (!bh)
  369. return -EIO;
  370. eh = (struct ext4_extent_header *)bh->b_data;
  371. if (eh->eh_depth != 0) {
  372. ix = EXT_FIRST_INDEX(eh);
  373. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  374. retval = free_ext_idx(handle, inode, ix);
  375. if (retval)
  376. break;
  377. }
  378. }
  379. put_bh(bh);
  380. extend_credit_for_blkdel(handle, inode);
  381. ext4_free_blocks(handle, inode, 0, block, 1,
  382. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  383. return retval;
  384. }
  385. /*
  386. * Free the extent meta data blocks only
  387. */
  388. static int free_ext_block(handle_t *handle, struct inode *inode)
  389. {
  390. int i, retval = 0;
  391. struct ext4_inode_info *ei = EXT4_I(inode);
  392. struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
  393. struct ext4_extent_idx *ix;
  394. if (eh->eh_depth == 0)
  395. /*
  396. * No extra blocks allocated for extent meta data
  397. */
  398. return 0;
  399. ix = EXT_FIRST_INDEX(eh);
  400. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
  401. retval = free_ext_idx(handle, inode, ix);
  402. if (retval)
  403. return retval;
  404. }
  405. return retval;
  406. }
  407. int ext4_ext_migrate(struct inode *inode)
  408. {
  409. handle_t *handle;
  410. int retval = 0, i;
  411. __le32 *i_data;
  412. ext4_lblk_t blk_count = 0;
  413. struct ext4_inode_info *ei;
  414. struct inode *tmp_inode = NULL;
  415. struct list_blocks_struct lb;
  416. unsigned long max_entries;
  417. __u32 goal;
  418. /*
  419. * If the filesystem does not support extents, or the inode
  420. * already is extent-based, error out.
  421. */
  422. if (!EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
  423. EXT4_FEATURE_INCOMPAT_EXTENTS) ||
  424. (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  425. return -EINVAL;
  426. if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
  427. /*
  428. * don't migrate fast symlink
  429. */
  430. return retval;
  431. handle = ext4_journal_start(inode,
  432. EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
  433. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  434. EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)
  435. + 1);
  436. if (IS_ERR(handle)) {
  437. retval = PTR_ERR(handle);
  438. return retval;
  439. }
  440. goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
  441. EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
  442. tmp_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  443. S_IFREG, 0, goal);
  444. if (IS_ERR(tmp_inode)) {
  445. retval = -ENOMEM;
  446. ext4_journal_stop(handle);
  447. return retval;
  448. }
  449. i_size_write(tmp_inode, i_size_read(inode));
  450. /*
  451. * We don't want the inode to be reclaimed
  452. * if we got interrupted in between. We have
  453. * this tmp inode carrying reference to the
  454. * data blocks of the original file. We set
  455. * the i_nlink to zero at the last stage after
  456. * switching the original file to extent format
  457. */
  458. tmp_inode->i_nlink = 1;
  459. ext4_ext_tree_init(handle, tmp_inode);
  460. ext4_orphan_add(handle, tmp_inode);
  461. ext4_journal_stop(handle);
  462. /*
  463. * start with one credit accounted for
  464. * superblock modification.
  465. *
  466. * For the tmp_inode we already have commited the
  467. * trascation that created the inode. Later as and
  468. * when we add extents we extent the journal
  469. */
  470. /*
  471. * Even though we take i_mutex we can still cause block
  472. * allocation via mmap write to holes. If we have allocated
  473. * new blocks we fail migrate. New block allocation will
  474. * clear EXT4_STATE_EXT_MIGRATE flag. The flag is updated
  475. * with i_data_sem held to prevent racing with block
  476. * allocation.
  477. */
  478. down_read((&EXT4_I(inode)->i_data_sem));
  479. EXT4_I(inode)->i_state |= EXT4_STATE_EXT_MIGRATE;
  480. up_read((&EXT4_I(inode)->i_data_sem));
  481. handle = ext4_journal_start(inode, 1);
  482. ei = EXT4_I(inode);
  483. i_data = ei->i_data;
  484. memset(&lb, 0, sizeof(lb));
  485. /* 32 bit block address 4 bytes */
  486. max_entries = inode->i_sb->s_blocksize >> 2;
  487. for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) {
  488. if (i_data[i]) {
  489. retval = update_extent_range(handle, tmp_inode,
  490. le32_to_cpu(i_data[i]),
  491. blk_count, &lb);
  492. if (retval)
  493. goto err_out;
  494. }
  495. }
  496. if (i_data[EXT4_IND_BLOCK]) {
  497. retval = update_ind_extent_range(handle, tmp_inode,
  498. le32_to_cpu(i_data[EXT4_IND_BLOCK]),
  499. &blk_count, &lb);
  500. if (retval)
  501. goto err_out;
  502. } else
  503. blk_count += max_entries;
  504. if (i_data[EXT4_DIND_BLOCK]) {
  505. retval = update_dind_extent_range(handle, tmp_inode,
  506. le32_to_cpu(i_data[EXT4_DIND_BLOCK]),
  507. &blk_count, &lb);
  508. if (retval)
  509. goto err_out;
  510. } else
  511. blk_count += max_entries * max_entries;
  512. if (i_data[EXT4_TIND_BLOCK]) {
  513. retval = update_tind_extent_range(handle, tmp_inode,
  514. le32_to_cpu(i_data[EXT4_TIND_BLOCK]),
  515. &blk_count, &lb);
  516. if (retval)
  517. goto err_out;
  518. }
  519. /*
  520. * Build the last extent
  521. */
  522. retval = finish_range(handle, tmp_inode, &lb);
  523. err_out:
  524. if (retval)
  525. /*
  526. * Failure case delete the extent information with the
  527. * tmp_inode
  528. */
  529. free_ext_block(handle, tmp_inode);
  530. else {
  531. retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
  532. if (retval)
  533. /*
  534. * if we fail to swap inode data free the extent
  535. * details of the tmp inode
  536. */
  537. free_ext_block(handle, tmp_inode);
  538. }
  539. /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
  540. if (ext4_journal_extend(handle, 1) != 0)
  541. ext4_journal_restart(handle, 1);
  542. /*
  543. * Mark the tmp_inode as of size zero
  544. */
  545. i_size_write(tmp_inode, 0);
  546. /*
  547. * set the i_blocks count to zero
  548. * so that the ext4_delete_inode does the
  549. * right job
  550. *
  551. * We don't need to take the i_lock because
  552. * the inode is not visible to user space.
  553. */
  554. tmp_inode->i_blocks = 0;
  555. /* Reset the extent details */
  556. ext4_ext_tree_init(handle, tmp_inode);
  557. /*
  558. * Set the i_nlink to zero so that
  559. * generic_drop_inode really deletes the
  560. * inode
  561. */
  562. tmp_inode->i_nlink = 0;
  563. ext4_journal_stop(handle);
  564. unlock_new_inode(tmp_inode);
  565. iput(tmp_inode);
  566. return retval;
  567. }