migrate.c 14 KB

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