migrate.c 14 KB

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