move_extent.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
  3. * Written by Takashi Sato <t-sato@yk.jp.nec.com>
  4. * Akira Fujita <a-fujita@rs.jp.nec.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2.1 of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/slab.h>
  18. #include "ext4_jbd2.h"
  19. #include "ext4.h"
  20. #include "ext4_extents.h"
  21. /**
  22. * get_ext_path - Find an extent path for designated logical block number.
  23. *
  24. * @inode: an inode which is searched
  25. * @lblock: logical block number to find an extent path
  26. * @path: pointer to an extent path pointer (for output)
  27. *
  28. * ext4_find_extent wrapper. Return 0 on success, or a negative error value
  29. * on failure.
  30. */
  31. static inline int
  32. get_ext_path(struct inode *inode, ext4_lblk_t lblock,
  33. struct ext4_ext_path **ppath)
  34. {
  35. struct ext4_ext_path *path;
  36. path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
  37. if (IS_ERR(path))
  38. return PTR_ERR(path);
  39. if (path[ext_depth(inode)].p_ext == NULL) {
  40. ext4_ext_drop_refs(path);
  41. kfree(path);
  42. *ppath = NULL;
  43. return -ENODATA;
  44. }
  45. *ppath = path;
  46. return 0;
  47. }
  48. /**
  49. * ext4_double_down_write_data_sem - Acquire two inodes' write lock
  50. * of i_data_sem
  51. *
  52. * Acquire write lock of i_data_sem of the two inodes
  53. */
  54. void
  55. ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
  56. {
  57. if (first < second) {
  58. down_write(&EXT4_I(first)->i_data_sem);
  59. down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER);
  60. } else {
  61. down_write(&EXT4_I(second)->i_data_sem);
  62. down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER);
  63. }
  64. }
  65. /**
  66. * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
  67. *
  68. * @orig_inode: original inode structure to be released its lock first
  69. * @donor_inode: donor inode structure to be released its lock second
  70. * Release write lock of i_data_sem of two inodes (orig and donor).
  71. */
  72. void
  73. ext4_double_up_write_data_sem(struct inode *orig_inode,
  74. struct inode *donor_inode)
  75. {
  76. up_write(&EXT4_I(orig_inode)->i_data_sem);
  77. up_write(&EXT4_I(donor_inode)->i_data_sem);
  78. }
  79. /**
  80. * mext_check_coverage - Check that all extents in range has the same type
  81. *
  82. * @inode: inode in question
  83. * @from: block offset of inode
  84. * @count: block count to be checked
  85. * @unwritten: extents expected to be unwritten
  86. * @err: pointer to save error value
  87. *
  88. * Return 1 if all extents in range has expected type, and zero otherwise.
  89. */
  90. static int
  91. mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
  92. int unwritten, int *err)
  93. {
  94. struct ext4_ext_path *path = NULL;
  95. struct ext4_extent *ext;
  96. int ret = 0;
  97. ext4_lblk_t last = from + count;
  98. while (from < last) {
  99. *err = get_ext_path(inode, from, &path);
  100. if (*err)
  101. goto out;
  102. ext = path[ext_depth(inode)].p_ext;
  103. if (unwritten != ext4_ext_is_unwritten(ext))
  104. goto out;
  105. from += ext4_ext_get_actual_len(ext);
  106. ext4_ext_drop_refs(path);
  107. }
  108. ret = 1;
  109. out:
  110. ext4_ext_drop_refs(path);
  111. kfree(path);
  112. return ret;
  113. }
  114. /**
  115. * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
  116. *
  117. * @inode1: the inode structure
  118. * @inode2: the inode structure
  119. * @index1: page index
  120. * @index2: page index
  121. * @page: result page vector
  122. *
  123. * Grab two locked pages for inode's by inode order
  124. */
  125. static int
  126. mext_page_double_lock(struct inode *inode1, struct inode *inode2,
  127. pgoff_t index1, pgoff_t index2, struct page *page[2])
  128. {
  129. struct address_space *mapping[2];
  130. unsigned fl = AOP_FLAG_NOFS;
  131. BUG_ON(!inode1 || !inode2);
  132. if (inode1 < inode2) {
  133. mapping[0] = inode1->i_mapping;
  134. mapping[1] = inode2->i_mapping;
  135. } else {
  136. pgoff_t tmp = index1;
  137. index1 = index2;
  138. index2 = tmp;
  139. mapping[0] = inode2->i_mapping;
  140. mapping[1] = inode1->i_mapping;
  141. }
  142. page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
  143. if (!page[0])
  144. return -ENOMEM;
  145. page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
  146. if (!page[1]) {
  147. unlock_page(page[0]);
  148. page_cache_release(page[0]);
  149. return -ENOMEM;
  150. }
  151. /*
  152. * grab_cache_page_write_begin() may not wait on page's writeback if
  153. * BDI not demand that. But it is reasonable to be very conservative
  154. * here and explicitly wait on page's writeback
  155. */
  156. wait_on_page_writeback(page[0]);
  157. wait_on_page_writeback(page[1]);
  158. if (inode1 > inode2)
  159. swap(page[0], page[1]);
  160. return 0;
  161. }
  162. /* Force page buffers uptodate w/o dropping page's lock */
  163. static int
  164. mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
  165. {
  166. struct inode *inode = page->mapping->host;
  167. sector_t block;
  168. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  169. unsigned int blocksize, block_start, block_end;
  170. int i, err, nr = 0, partial = 0;
  171. BUG_ON(!PageLocked(page));
  172. BUG_ON(PageWriteback(page));
  173. if (PageUptodate(page))
  174. return 0;
  175. blocksize = i_blocksize(inode);
  176. if (!page_has_buffers(page))
  177. create_empty_buffers(page, blocksize, 0);
  178. head = page_buffers(page);
  179. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  180. for (bh = head, block_start = 0; bh != head || !block_start;
  181. block++, block_start = block_end, bh = bh->b_this_page) {
  182. block_end = block_start + blocksize;
  183. if (block_end <= from || block_start >= to) {
  184. if (!buffer_uptodate(bh))
  185. partial = 1;
  186. continue;
  187. }
  188. if (buffer_uptodate(bh))
  189. continue;
  190. if (!buffer_mapped(bh)) {
  191. err = ext4_get_block(inode, block, bh, 0);
  192. if (err) {
  193. SetPageError(page);
  194. return err;
  195. }
  196. if (!buffer_mapped(bh)) {
  197. zero_user(page, block_start, blocksize);
  198. set_buffer_uptodate(bh);
  199. continue;
  200. }
  201. }
  202. BUG_ON(nr >= MAX_BUF_PER_PAGE);
  203. arr[nr++] = bh;
  204. }
  205. /* No io required */
  206. if (!nr)
  207. goto out;
  208. for (i = 0; i < nr; i++) {
  209. bh = arr[i];
  210. if (!bh_uptodate_or_lock(bh)) {
  211. err = bh_submit_read(bh);
  212. if (err)
  213. return err;
  214. }
  215. }
  216. out:
  217. if (!partial)
  218. SetPageUptodate(page);
  219. return 0;
  220. }
  221. /**
  222. * move_extent_per_page - Move extent data per page
  223. *
  224. * @o_filp: file structure of original file
  225. * @donor_inode: donor inode
  226. * @orig_page_offset: page index on original file
  227. * @donor_page_offset: page index on donor file
  228. * @data_offset_in_page: block index where data swapping starts
  229. * @block_len_in_page: the number of blocks to be swapped
  230. * @unwritten: orig extent is unwritten or not
  231. * @err: pointer to save return value
  232. *
  233. * Save the data in original inode blocks and replace original inode extents
  234. * with donor inode extents by calling ext4_swap_extents().
  235. * Finally, write out the saved data in new original inode blocks. Return
  236. * replaced block count.
  237. */
  238. static int
  239. move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
  240. pgoff_t orig_page_offset, pgoff_t donor_page_offset,
  241. int data_offset_in_page,
  242. int block_len_in_page, int unwritten, int *err)
  243. {
  244. struct inode *orig_inode = file_inode(o_filp);
  245. struct page *pagep[2] = {NULL, NULL};
  246. handle_t *handle;
  247. ext4_lblk_t orig_blk_offset, donor_blk_offset;
  248. unsigned long blocksize = orig_inode->i_sb->s_blocksize;
  249. unsigned int tmp_data_size, data_size, replaced_size;
  250. int i, err2, jblocks, retries = 0;
  251. int replaced_count = 0;
  252. int from = data_offset_in_page << orig_inode->i_blkbits;
  253. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  254. struct super_block *sb = orig_inode->i_sb;
  255. struct buffer_head *bh = NULL;
  256. /*
  257. * It needs twice the amount of ordinary journal buffers because
  258. * inode and donor_inode may change each different metadata blocks.
  259. */
  260. again:
  261. *err = 0;
  262. jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
  263. handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
  264. if (IS_ERR(handle)) {
  265. *err = PTR_ERR(handle);
  266. return 0;
  267. }
  268. orig_blk_offset = orig_page_offset * blocks_per_page +
  269. data_offset_in_page;
  270. donor_blk_offset = donor_page_offset * blocks_per_page +
  271. data_offset_in_page;
  272. /* Calculate data_size */
  273. if ((orig_blk_offset + block_len_in_page - 1) ==
  274. ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
  275. /* Replace the last block */
  276. tmp_data_size = orig_inode->i_size & (blocksize - 1);
  277. /*
  278. * If data_size equal zero, it shows data_size is multiples of
  279. * blocksize. So we set appropriate value.
  280. */
  281. if (tmp_data_size == 0)
  282. tmp_data_size = blocksize;
  283. data_size = tmp_data_size +
  284. ((block_len_in_page - 1) << orig_inode->i_blkbits);
  285. } else
  286. data_size = block_len_in_page << orig_inode->i_blkbits;
  287. replaced_size = data_size;
  288. *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
  289. donor_page_offset, pagep);
  290. if (unlikely(*err < 0))
  291. goto stop_journal;
  292. /*
  293. * If orig extent was unwritten it can become initialized
  294. * at any time after i_data_sem was dropped, in order to
  295. * serialize with delalloc we have recheck extent while we
  296. * hold page's lock, if it is still the case data copy is not
  297. * necessary, just swap data blocks between orig and donor.
  298. */
  299. if (unwritten) {
  300. ext4_double_down_write_data_sem(orig_inode, donor_inode);
  301. /* If any of extents in range became initialized we have to
  302. * fallback to data copying */
  303. unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
  304. block_len_in_page, 1, err);
  305. if (*err)
  306. goto drop_data_sem;
  307. unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
  308. block_len_in_page, 1, err);
  309. if (*err)
  310. goto drop_data_sem;
  311. if (!unwritten) {
  312. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  313. goto data_copy;
  314. }
  315. if ((page_has_private(pagep[0]) &&
  316. !try_to_release_page(pagep[0], 0)) ||
  317. (page_has_private(pagep[1]) &&
  318. !try_to_release_page(pagep[1], 0))) {
  319. *err = -EBUSY;
  320. goto drop_data_sem;
  321. }
  322. replaced_count = ext4_swap_extents(handle, orig_inode,
  323. donor_inode, orig_blk_offset,
  324. donor_blk_offset,
  325. block_len_in_page, 1, err);
  326. drop_data_sem:
  327. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  328. goto unlock_pages;
  329. }
  330. data_copy:
  331. *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
  332. if (*err)
  333. goto unlock_pages;
  334. /* At this point all buffers in range are uptodate, old mapping layout
  335. * is no longer required, try to drop it now. */
  336. if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
  337. (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
  338. *err = -EBUSY;
  339. goto unlock_pages;
  340. }
  341. ext4_double_down_write_data_sem(orig_inode, donor_inode);
  342. replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
  343. orig_blk_offset, donor_blk_offset,
  344. block_len_in_page, 1, err);
  345. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  346. if (*err) {
  347. if (replaced_count) {
  348. block_len_in_page = replaced_count;
  349. replaced_size =
  350. block_len_in_page << orig_inode->i_blkbits;
  351. } else
  352. goto unlock_pages;
  353. }
  354. /* Perform all necessary steps similar write_begin()/write_end()
  355. * but keeping in mind that i_size will not change */
  356. if (!page_has_buffers(pagep[0]))
  357. create_empty_buffers(pagep[0], 1 << orig_inode->i_blkbits, 0);
  358. bh = page_buffers(pagep[0]);
  359. for (i = 0; i < data_offset_in_page; i++)
  360. bh = bh->b_this_page;
  361. for (i = 0; i < block_len_in_page; i++) {
  362. *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
  363. if (*err < 0)
  364. break;
  365. bh = bh->b_this_page;
  366. }
  367. if (!*err)
  368. *err = block_commit_write(pagep[0], from, from + replaced_size);
  369. if (unlikely(*err < 0))
  370. goto repair_branches;
  371. /* Even in case of data=writeback it is reasonable to pin
  372. * inode to transaction, to prevent unexpected data loss */
  373. *err = ext4_jbd2_file_inode(handle, orig_inode);
  374. unlock_pages:
  375. unlock_page(pagep[0]);
  376. page_cache_release(pagep[0]);
  377. unlock_page(pagep[1]);
  378. page_cache_release(pagep[1]);
  379. stop_journal:
  380. ext4_journal_stop(handle);
  381. if (*err == -ENOSPC &&
  382. ext4_should_retry_alloc(sb, &retries))
  383. goto again;
  384. /* Buffer was busy because probably is pinned to journal transaction,
  385. * force transaction commit may help to free it. */
  386. if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
  387. jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
  388. goto again;
  389. return replaced_count;
  390. repair_branches:
  391. /*
  392. * This should never ever happen!
  393. * Extents are swapped already, but we are not able to copy data.
  394. * Try to swap extents to it's original places
  395. */
  396. ext4_double_down_write_data_sem(orig_inode, donor_inode);
  397. replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
  398. orig_blk_offset, donor_blk_offset,
  399. block_len_in_page, 0, &err2);
  400. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  401. if (replaced_count != block_len_in_page) {
  402. EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
  403. "Unable to copy data block,"
  404. " data will be lost.");
  405. *err = -EIO;
  406. }
  407. replaced_count = 0;
  408. goto unlock_pages;
  409. }
  410. /**
  411. * mext_check_arguments - Check whether move extent can be done
  412. *
  413. * @orig_inode: original inode
  414. * @donor_inode: donor inode
  415. * @orig_start: logical start offset in block for orig
  416. * @donor_start: logical start offset in block for donor
  417. * @len: the number of blocks to be moved
  418. *
  419. * Check the arguments of ext4_move_extents() whether the files can be
  420. * exchanged with each other.
  421. * Return 0 on success, or a negative error value on failure.
  422. */
  423. static int
  424. mext_check_arguments(struct inode *orig_inode,
  425. struct inode *donor_inode, __u64 orig_start,
  426. __u64 donor_start, __u64 *len)
  427. {
  428. __u64 orig_eof, donor_eof;
  429. unsigned int blkbits = orig_inode->i_blkbits;
  430. unsigned int blocksize = 1 << blkbits;
  431. orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
  432. donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
  433. if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
  434. ext4_debug("ext4 move extent: suid or sgid is set"
  435. " to donor file [ino:orig %lu, donor %lu]\n",
  436. orig_inode->i_ino, donor_inode->i_ino);
  437. return -EINVAL;
  438. }
  439. if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
  440. return -EPERM;
  441. /* Ext4 move extent does not support swapfile */
  442. if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
  443. ext4_debug("ext4 move extent: The argument files should "
  444. "not be swapfile [ino:orig %lu, donor %lu]\n",
  445. orig_inode->i_ino, donor_inode->i_ino);
  446. return -EBUSY;
  447. }
  448. if (IS_NOQUOTA(orig_inode) || IS_NOQUOTA(donor_inode)) {
  449. ext4_debug("ext4 move extent: The argument files should "
  450. "not be quota files [ino:orig %lu, donor %lu]\n",
  451. orig_inode->i_ino, donor_inode->i_ino);
  452. return -EBUSY;
  453. }
  454. /* Ext4 move extent supports only extent based file */
  455. if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
  456. ext4_debug("ext4 move extent: orig file is not extents "
  457. "based file [ino:orig %lu]\n", orig_inode->i_ino);
  458. return -EOPNOTSUPP;
  459. } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
  460. ext4_debug("ext4 move extent: donor file is not extents "
  461. "based file [ino:donor %lu]\n", donor_inode->i_ino);
  462. return -EOPNOTSUPP;
  463. }
  464. if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
  465. ext4_debug("ext4 move extent: File size is 0 byte\n");
  466. return -EINVAL;
  467. }
  468. /* Start offset should be same */
  469. if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
  470. (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
  471. ext4_debug("ext4 move extent: orig and donor's start "
  472. "offset are not alligned [ino:orig %lu, donor %lu]\n",
  473. orig_inode->i_ino, donor_inode->i_ino);
  474. return -EINVAL;
  475. }
  476. if ((orig_start >= EXT_MAX_BLOCKS) ||
  477. (donor_start >= EXT_MAX_BLOCKS) ||
  478. (*len > EXT_MAX_BLOCKS) ||
  479. (donor_start + *len >= EXT_MAX_BLOCKS) ||
  480. (orig_start + *len >= EXT_MAX_BLOCKS)) {
  481. ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
  482. "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
  483. orig_inode->i_ino, donor_inode->i_ino);
  484. return -EINVAL;
  485. }
  486. if (orig_eof <= orig_start)
  487. *len = 0;
  488. else if (orig_eof < orig_start + *len - 1)
  489. *len = orig_eof - orig_start;
  490. if (donor_eof <= donor_start)
  491. *len = 0;
  492. else if (donor_eof < donor_start + *len - 1)
  493. *len = donor_eof - donor_start;
  494. if (!*len) {
  495. ext4_debug("ext4 move extent: len should not be 0 "
  496. "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
  497. donor_inode->i_ino);
  498. return -EINVAL;
  499. }
  500. return 0;
  501. }
  502. /**
  503. * ext4_move_extents - Exchange the specified range of a file
  504. *
  505. * @o_filp: file structure of the original file
  506. * @d_filp: file structure of the donor file
  507. * @orig_blk: start offset in block for orig
  508. * @donor_blk: start offset in block for donor
  509. * @len: the number of blocks to be moved
  510. * @moved_len: moved block length
  511. *
  512. * This function returns 0 and moved block length is set in moved_len
  513. * if succeed, otherwise returns error value.
  514. *
  515. */
  516. int
  517. ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
  518. __u64 donor_blk, __u64 len, __u64 *moved_len)
  519. {
  520. struct inode *orig_inode = file_inode(o_filp);
  521. struct inode *donor_inode = file_inode(d_filp);
  522. struct ext4_ext_path *path = NULL;
  523. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  524. ext4_lblk_t o_end, o_start = orig_blk;
  525. ext4_lblk_t d_start = donor_blk;
  526. int ret;
  527. if (orig_inode->i_sb != donor_inode->i_sb) {
  528. ext4_debug("ext4 move extent: The argument files "
  529. "should be in same FS [ino:orig %lu, donor %lu]\n",
  530. orig_inode->i_ino, donor_inode->i_ino);
  531. return -EINVAL;
  532. }
  533. /* orig and donor should be different inodes */
  534. if (orig_inode == donor_inode) {
  535. ext4_debug("ext4 move extent: The argument files should not "
  536. "be same inode [ino:orig %lu, donor %lu]\n",
  537. orig_inode->i_ino, donor_inode->i_ino);
  538. return -EINVAL;
  539. }
  540. /* Regular file check */
  541. if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
  542. ext4_debug("ext4 move extent: The argument files should be "
  543. "regular file [ino:orig %lu, donor %lu]\n",
  544. orig_inode->i_ino, donor_inode->i_ino);
  545. return -EINVAL;
  546. }
  547. /* TODO: it's not obvious how to swap blocks for inodes with full
  548. journaling enabled */
  549. if (ext4_should_journal_data(orig_inode) ||
  550. ext4_should_journal_data(donor_inode)) {
  551. ext4_msg(orig_inode->i_sb, KERN_ERR,
  552. "Online defrag not supported with data journaling");
  553. return -EOPNOTSUPP;
  554. }
  555. if (ext4_encrypted_inode(orig_inode) ||
  556. ext4_encrypted_inode(donor_inode)) {
  557. ext4_msg(orig_inode->i_sb, KERN_ERR,
  558. "Online defrag not supported for encrypted files");
  559. return -EOPNOTSUPP;
  560. }
  561. /* Protect orig and donor inodes against a truncate */
  562. lock_two_nondirectories(orig_inode, donor_inode);
  563. /* Wait for all existing dio workers */
  564. ext4_inode_block_unlocked_dio(orig_inode);
  565. ext4_inode_block_unlocked_dio(donor_inode);
  566. inode_dio_wait(orig_inode);
  567. inode_dio_wait(donor_inode);
  568. /* Protect extent tree against block allocations via delalloc */
  569. ext4_double_down_write_data_sem(orig_inode, donor_inode);
  570. /* Check the filesystem environment whether move_extent can be done */
  571. ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
  572. donor_blk, &len);
  573. if (ret)
  574. goto out;
  575. o_end = o_start + len;
  576. while (o_start < o_end) {
  577. struct ext4_extent *ex;
  578. ext4_lblk_t cur_blk, next_blk;
  579. pgoff_t orig_page_index, donor_page_index;
  580. int offset_in_page;
  581. int unwritten, cur_len;
  582. ret = get_ext_path(orig_inode, o_start, &path);
  583. if (ret)
  584. goto out;
  585. ex = path[path->p_depth].p_ext;
  586. next_blk = ext4_ext_next_allocated_block(path);
  587. cur_blk = le32_to_cpu(ex->ee_block);
  588. cur_len = ext4_ext_get_actual_len(ex);
  589. /* Check hole before the start pos */
  590. if (cur_blk + cur_len - 1 < o_start) {
  591. if (next_blk == EXT_MAX_BLOCKS) {
  592. o_start = o_end;
  593. ret = -ENODATA;
  594. goto out;
  595. }
  596. d_start += next_blk - o_start;
  597. o_start = next_blk;
  598. continue;
  599. /* Check hole after the start pos */
  600. } else if (cur_blk > o_start) {
  601. /* Skip hole */
  602. d_start += cur_blk - o_start;
  603. o_start = cur_blk;
  604. /* Extent inside requested range ?*/
  605. if (cur_blk >= o_end)
  606. goto out;
  607. } else { /* in_range(o_start, o_blk, o_len) */
  608. cur_len += cur_blk - o_start;
  609. }
  610. unwritten = ext4_ext_is_unwritten(ex);
  611. if (o_end - o_start < cur_len)
  612. cur_len = o_end - o_start;
  613. orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
  614. orig_inode->i_blkbits);
  615. donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
  616. donor_inode->i_blkbits);
  617. offset_in_page = o_start % blocks_per_page;
  618. if (cur_len > blocks_per_page- offset_in_page)
  619. cur_len = blocks_per_page - offset_in_page;
  620. /*
  621. * Up semaphore to avoid following problems:
  622. * a. transaction deadlock among ext4_journal_start,
  623. * ->write_begin via pagefault, and jbd2_journal_commit
  624. * b. racing with ->readpage, ->write_begin, and ext4_get_block
  625. * in move_extent_per_page
  626. */
  627. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  628. /* Swap original branches with new branches */
  629. move_extent_per_page(o_filp, donor_inode,
  630. orig_page_index, donor_page_index,
  631. offset_in_page, cur_len,
  632. unwritten, &ret);
  633. ext4_double_down_write_data_sem(orig_inode, donor_inode);
  634. if (ret < 0)
  635. break;
  636. o_start += cur_len;
  637. d_start += cur_len;
  638. }
  639. *moved_len = o_start - orig_blk;
  640. if (*moved_len > len)
  641. *moved_len = len;
  642. out:
  643. if (*moved_len) {
  644. ext4_discard_preallocations(orig_inode);
  645. ext4_discard_preallocations(donor_inode);
  646. }
  647. ext4_ext_drop_refs(path);
  648. kfree(path);
  649. ext4_double_up_write_data_sem(orig_inode, donor_inode);
  650. ext4_inode_resume_unlocked_dio(orig_inode);
  651. ext4_inode_resume_unlocked_dio(donor_inode);
  652. unlock_two_nondirectories(orig_inode, donor_inode);
  653. return ret;
  654. }