move_extent.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 "ext4_jbd2.h"
  18. #include "ext4_extents.h"
  19. #include "ext4.h"
  20. #define get_ext_path(path, inode, block, ret) \
  21. do { \
  22. path = ext4_ext_find_extent(inode, block, path); \
  23. if (IS_ERR(path)) { \
  24. ret = PTR_ERR(path); \
  25. path = NULL; \
  26. } \
  27. } while (0)
  28. /**
  29. * copy_extent_status - Copy the extent's initialization status
  30. *
  31. * @src: an extent for getting initialize status
  32. * @dest: an extent to be set the status
  33. */
  34. static void
  35. copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
  36. {
  37. if (ext4_ext_is_uninitialized(src))
  38. ext4_ext_mark_uninitialized(dest);
  39. else
  40. dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
  41. }
  42. /**
  43. * mext_next_extent - Search for the next extent and set it to "extent"
  44. *
  45. * @inode: inode which is searched
  46. * @path: this will obtain data for the next extent
  47. * @extent: pointer to the next extent we have just gotten
  48. *
  49. * Search the next extent in the array of ext4_ext_path structure (@path)
  50. * and set it to ext4_extent structure (@extent). In addition, the member of
  51. * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
  52. * ext4_ext_path structure refers to the last extent, or a negative error
  53. * value on failure.
  54. */
  55. static int
  56. mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
  57. struct ext4_extent **extent)
  58. {
  59. int ppos, leaf_ppos = path->p_depth;
  60. ppos = leaf_ppos;
  61. if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
  62. /* leaf block */
  63. *extent = ++path[ppos].p_ext;
  64. return 0;
  65. }
  66. while (--ppos >= 0) {
  67. if (EXT_LAST_INDEX(path[ppos].p_hdr) >
  68. path[ppos].p_idx) {
  69. int cur_ppos = ppos;
  70. /* index block */
  71. path[ppos].p_idx++;
  72. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  73. if (path[ppos+1].p_bh)
  74. brelse(path[ppos+1].p_bh);
  75. path[ppos+1].p_bh =
  76. sb_bread(inode->i_sb, path[ppos].p_block);
  77. if (!path[ppos+1].p_bh)
  78. return -EIO;
  79. path[ppos+1].p_hdr =
  80. ext_block_hdr(path[ppos+1].p_bh);
  81. /* Halfway index block */
  82. while (++cur_ppos < leaf_ppos) {
  83. path[cur_ppos].p_idx =
  84. EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
  85. path[cur_ppos].p_block =
  86. idx_pblock(path[cur_ppos].p_idx);
  87. if (path[cur_ppos+1].p_bh)
  88. brelse(path[cur_ppos+1].p_bh);
  89. path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
  90. path[cur_ppos].p_block);
  91. if (!path[cur_ppos+1].p_bh)
  92. return -EIO;
  93. path[cur_ppos+1].p_hdr =
  94. ext_block_hdr(path[cur_ppos+1].p_bh);
  95. }
  96. /* leaf block */
  97. path[leaf_ppos].p_ext = *extent =
  98. EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
  99. return 0;
  100. }
  101. }
  102. /* We found the last extent */
  103. return 1;
  104. }
  105. /**
  106. * mext_double_down_read - Acquire two inodes' read semaphore
  107. *
  108. * @orig_inode: original inode structure
  109. * @donor_inode: donor inode structure
  110. * Acquire read semaphore of the two inodes (orig and donor) by i_ino order.
  111. */
  112. static void
  113. mext_double_down_read(struct inode *orig_inode, struct inode *donor_inode)
  114. {
  115. struct inode *first = orig_inode, *second = donor_inode;
  116. BUG_ON(orig_inode == NULL || donor_inode == NULL);
  117. /*
  118. * Use the inode number to provide the stable locking order instead
  119. * of its address, because the C language doesn't guarantee you can
  120. * compare pointers that don't come from the same array.
  121. */
  122. if (donor_inode->i_ino < orig_inode->i_ino) {
  123. first = donor_inode;
  124. second = orig_inode;
  125. }
  126. down_read(&EXT4_I(first)->i_data_sem);
  127. down_read(&EXT4_I(second)->i_data_sem);
  128. }
  129. /**
  130. * mext_double_down_write - Acquire two inodes' write semaphore
  131. *
  132. * @orig_inode: original inode structure
  133. * @donor_inode: donor inode structure
  134. * Acquire write semaphore of the two inodes (orig and donor) by i_ino order.
  135. */
  136. static void
  137. mext_double_down_write(struct inode *orig_inode, struct inode *donor_inode)
  138. {
  139. struct inode *first = orig_inode, *second = donor_inode;
  140. BUG_ON(orig_inode == NULL || donor_inode == NULL);
  141. /*
  142. * Use the inode number to provide the stable locking order instead
  143. * of its address, because the C language doesn't guarantee you can
  144. * compare pointers that don't come from the same array.
  145. */
  146. if (donor_inode->i_ino < orig_inode->i_ino) {
  147. first = donor_inode;
  148. second = orig_inode;
  149. }
  150. down_write(&EXT4_I(first)->i_data_sem);
  151. down_write(&EXT4_I(second)->i_data_sem);
  152. }
  153. /**
  154. * mext_double_up_read - Release two inodes' read semaphore
  155. *
  156. * @orig_inode: original inode structure to be released its lock first
  157. * @donor_inode: donor inode structure to be released its lock second
  158. * Release read semaphore of two inodes (orig and donor).
  159. */
  160. static void
  161. mext_double_up_read(struct inode *orig_inode, struct inode *donor_inode)
  162. {
  163. BUG_ON(orig_inode == NULL || donor_inode == NULL);
  164. up_read(&EXT4_I(orig_inode)->i_data_sem);
  165. up_read(&EXT4_I(donor_inode)->i_data_sem);
  166. }
  167. /**
  168. * mext_double_up_write - Release two inodes' write semaphore
  169. *
  170. * @orig_inode: original inode structure to be released its lock first
  171. * @donor_inode: donor inode structure to be released its lock second
  172. * Release write semaphore of two inodes (orig and donor).
  173. */
  174. static void
  175. mext_double_up_write(struct inode *orig_inode, struct inode *donor_inode)
  176. {
  177. BUG_ON(orig_inode == NULL || donor_inode == NULL);
  178. up_write(&EXT4_I(orig_inode)->i_data_sem);
  179. up_write(&EXT4_I(donor_inode)->i_data_sem);
  180. }
  181. /**
  182. * mext_insert_across_blocks - Insert extents across leaf block
  183. *
  184. * @handle: journal handle
  185. * @orig_inode: original inode
  186. * @o_start: first original extent to be changed
  187. * @o_end: last original extent to be changed
  188. * @start_ext: first new extent to be inserted
  189. * @new_ext: middle of new extent to be inserted
  190. * @end_ext: last new extent to be inserted
  191. *
  192. * Allocate a new leaf block and insert extents into it. Return 0 on success,
  193. * or a negative error value on failure.
  194. */
  195. static int
  196. mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
  197. struct ext4_extent *o_start, struct ext4_extent *o_end,
  198. struct ext4_extent *start_ext, struct ext4_extent *new_ext,
  199. struct ext4_extent *end_ext)
  200. {
  201. struct ext4_ext_path *orig_path = NULL;
  202. ext4_lblk_t eblock = 0;
  203. int new_flag = 0;
  204. int end_flag = 0;
  205. int err = 0;
  206. if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
  207. if (o_start == o_end) {
  208. /* start_ext new_ext end_ext
  209. * donor |---------|-----------|--------|
  210. * orig |------------------------------|
  211. */
  212. end_flag = 1;
  213. } else {
  214. /* start_ext new_ext end_ext
  215. * donor |---------|----------|---------|
  216. * orig |---------------|--------------|
  217. */
  218. o_end->ee_block = end_ext->ee_block;
  219. o_end->ee_len = end_ext->ee_len;
  220. ext4_ext_store_pblock(o_end, ext_pblock(end_ext));
  221. }
  222. o_start->ee_len = start_ext->ee_len;
  223. new_flag = 1;
  224. } else if (start_ext->ee_len && new_ext->ee_len &&
  225. !end_ext->ee_len && o_start == o_end) {
  226. /* start_ext new_ext
  227. * donor |--------------|---------------|
  228. * orig |------------------------------|
  229. */
  230. o_start->ee_len = start_ext->ee_len;
  231. new_flag = 1;
  232. } else if (!start_ext->ee_len && new_ext->ee_len &&
  233. end_ext->ee_len && o_start == o_end) {
  234. /* new_ext end_ext
  235. * donor |--------------|---------------|
  236. * orig |------------------------------|
  237. */
  238. o_end->ee_block = end_ext->ee_block;
  239. o_end->ee_len = end_ext->ee_len;
  240. ext4_ext_store_pblock(o_end, ext_pblock(end_ext));
  241. /*
  242. * Set 0 to the extent block if new_ext was
  243. * the first block.
  244. */
  245. if (new_ext->ee_block)
  246. eblock = le32_to_cpu(new_ext->ee_block);
  247. new_flag = 1;
  248. } else {
  249. ext4_debug("ext4 move extent: Unexpected insert case\n");
  250. return -EIO;
  251. }
  252. if (new_flag) {
  253. get_ext_path(orig_path, orig_inode, eblock, err);
  254. if (orig_path == NULL)
  255. goto out;
  256. if (ext4_ext_insert_extent(handle, orig_inode,
  257. orig_path, new_ext))
  258. goto out;
  259. }
  260. if (end_flag) {
  261. get_ext_path(orig_path, orig_inode,
  262. le32_to_cpu(end_ext->ee_block) - 1, err);
  263. if (orig_path == NULL)
  264. goto out;
  265. if (ext4_ext_insert_extent(handle, orig_inode,
  266. orig_path, end_ext))
  267. goto out;
  268. }
  269. out:
  270. if (orig_path) {
  271. ext4_ext_drop_refs(orig_path);
  272. kfree(orig_path);
  273. }
  274. return err;
  275. }
  276. /**
  277. * mext_insert_inside_block - Insert new extent to the extent block
  278. *
  279. * @o_start: first original extent to be moved
  280. * @o_end: last original extent to be moved
  281. * @start_ext: first new extent to be inserted
  282. * @new_ext: middle of new extent to be inserted
  283. * @end_ext: last new extent to be inserted
  284. * @eh: extent header of target leaf block
  285. * @range_to_move: used to decide how to insert extent
  286. *
  287. * Insert extents into the leaf block. The extent (@o_start) is overwritten
  288. * by inserted extents.
  289. */
  290. static void
  291. mext_insert_inside_block(struct ext4_extent *o_start,
  292. struct ext4_extent *o_end,
  293. struct ext4_extent *start_ext,
  294. struct ext4_extent *new_ext,
  295. struct ext4_extent *end_ext,
  296. struct ext4_extent_header *eh,
  297. int range_to_move)
  298. {
  299. int i = 0;
  300. unsigned long len;
  301. /* Move the existing extents */
  302. if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
  303. len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
  304. (unsigned long)(o_end + 1);
  305. memmove(o_end + 1 + range_to_move, o_end + 1, len);
  306. }
  307. /* Insert start entry */
  308. if (start_ext->ee_len)
  309. o_start[i++].ee_len = start_ext->ee_len;
  310. /* Insert new entry */
  311. if (new_ext->ee_len) {
  312. o_start[i] = *new_ext;
  313. ext4_ext_store_pblock(&o_start[i++], ext_pblock(new_ext));
  314. }
  315. /* Insert end entry */
  316. if (end_ext->ee_len)
  317. o_start[i] = *end_ext;
  318. /* Increment the total entries counter on the extent block */
  319. le16_add_cpu(&eh->eh_entries, range_to_move);
  320. }
  321. /**
  322. * mext_insert_extents - Insert new extent
  323. *
  324. * @handle: journal handle
  325. * @orig_inode: original inode
  326. * @orig_path: path indicates first extent to be changed
  327. * @o_start: first original extent to be changed
  328. * @o_end: last original extent to be changed
  329. * @start_ext: first new extent to be inserted
  330. * @new_ext: middle of new extent to be inserted
  331. * @end_ext: last new extent to be inserted
  332. *
  333. * Call the function to insert extents. If we cannot add more extents into
  334. * the leaf block, we call mext_insert_across_blocks() to create a
  335. * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
  336. * on success, or a negative error value on failure.
  337. */
  338. static int
  339. mext_insert_extents(handle_t *handle, struct inode *orig_inode,
  340. struct ext4_ext_path *orig_path,
  341. struct ext4_extent *o_start,
  342. struct ext4_extent *o_end,
  343. struct ext4_extent *start_ext,
  344. struct ext4_extent *new_ext,
  345. struct ext4_extent *end_ext)
  346. {
  347. struct ext4_extent_header *eh;
  348. unsigned long need_slots, slots_range;
  349. int range_to_move, depth, ret;
  350. /*
  351. * The extents need to be inserted
  352. * start_extent + new_extent + end_extent.
  353. */
  354. need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
  355. (new_ext->ee_len ? 1 : 0);
  356. /* The number of slots between start and end */
  357. slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
  358. / sizeof(struct ext4_extent);
  359. /* Range to move the end of extent */
  360. range_to_move = need_slots - slots_range;
  361. depth = orig_path->p_depth;
  362. orig_path += depth;
  363. eh = orig_path->p_hdr;
  364. if (depth) {
  365. /* Register to journal */
  366. ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
  367. if (ret)
  368. return ret;
  369. }
  370. /* Expansion */
  371. if (range_to_move > 0 &&
  372. (range_to_move > le16_to_cpu(eh->eh_max)
  373. - le16_to_cpu(eh->eh_entries))) {
  374. ret = mext_insert_across_blocks(handle, orig_inode, o_start,
  375. o_end, start_ext, new_ext, end_ext);
  376. if (ret < 0)
  377. return ret;
  378. } else
  379. mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
  380. end_ext, eh, range_to_move);
  381. if (depth) {
  382. ret = ext4_handle_dirty_metadata(handle, orig_inode,
  383. orig_path->p_bh);
  384. if (ret)
  385. return ret;
  386. } else {
  387. ret = ext4_mark_inode_dirty(handle, orig_inode);
  388. if (ret < 0)
  389. return ret;
  390. }
  391. return 0;
  392. }
  393. /**
  394. * mext_leaf_block - Move one leaf extent block into the inode.
  395. *
  396. * @handle: journal handle
  397. * @orig_inode: original inode
  398. * @orig_path: path indicates first extent to be changed
  399. * @dext: donor extent
  400. * @from: start offset on the target file
  401. *
  402. * In order to insert extents into the leaf block, we must divide the extent
  403. * in the leaf block into three extents. The one is located to be inserted
  404. * extents, and the others are located around it.
  405. *
  406. * Therefore, this function creates structures to save extents of the leaf
  407. * block, and inserts extents by calling mext_insert_extents() with
  408. * created extents. Return 0 on success, or a negative error value on failure.
  409. */
  410. static int
  411. mext_leaf_block(handle_t *handle, struct inode *orig_inode,
  412. struct ext4_ext_path *orig_path, struct ext4_extent *dext,
  413. ext4_lblk_t *from)
  414. {
  415. struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
  416. struct ext4_extent new_ext, start_ext, end_ext;
  417. ext4_lblk_t new_ext_end;
  418. ext4_fsblk_t new_phys_end;
  419. int oext_alen, new_ext_alen, end_ext_alen;
  420. int depth = ext_depth(orig_inode);
  421. int ret;
  422. o_start = o_end = oext = orig_path[depth].p_ext;
  423. oext_alen = ext4_ext_get_actual_len(oext);
  424. start_ext.ee_len = end_ext.ee_len = 0;
  425. new_ext.ee_block = cpu_to_le32(*from);
  426. ext4_ext_store_pblock(&new_ext, ext_pblock(dext));
  427. new_ext.ee_len = dext->ee_len;
  428. new_ext_alen = ext4_ext_get_actual_len(&new_ext);
  429. new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
  430. new_phys_end = ext_pblock(&new_ext) + new_ext_alen - 1;
  431. /*
  432. * Case: original extent is first
  433. * oext |--------|
  434. * new_ext |--|
  435. * start_ext |--|
  436. */
  437. if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
  438. le32_to_cpu(new_ext.ee_block) <
  439. le32_to_cpu(oext->ee_block) + oext_alen) {
  440. start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
  441. le32_to_cpu(oext->ee_block));
  442. copy_extent_status(oext, &start_ext);
  443. } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
  444. prev_ext = oext - 1;
  445. /*
  446. * We can merge new_ext into previous extent,
  447. * if these are contiguous and same extent type.
  448. */
  449. if (ext4_can_extents_be_merged(orig_inode, prev_ext,
  450. &new_ext)) {
  451. o_start = prev_ext;
  452. start_ext.ee_len = cpu_to_le16(
  453. ext4_ext_get_actual_len(prev_ext) +
  454. new_ext_alen);
  455. copy_extent_status(prev_ext, &start_ext);
  456. new_ext.ee_len = 0;
  457. }
  458. }
  459. /*
  460. * Case: new_ext_end must be less than oext
  461. * oext |-----------|
  462. * new_ext |-------|
  463. */
  464. BUG_ON(le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end);
  465. /*
  466. * Case: new_ext is smaller than original extent
  467. * oext |---------------|
  468. * new_ext |-----------|
  469. * end_ext |---|
  470. */
  471. if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
  472. new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
  473. end_ext.ee_len =
  474. cpu_to_le16(le32_to_cpu(oext->ee_block) +
  475. oext_alen - 1 - new_ext_end);
  476. copy_extent_status(oext, &end_ext);
  477. end_ext_alen = ext4_ext_get_actual_len(&end_ext);
  478. ext4_ext_store_pblock(&end_ext,
  479. (ext_pblock(o_end) + oext_alen - end_ext_alen));
  480. end_ext.ee_block =
  481. cpu_to_le32(le32_to_cpu(o_end->ee_block) +
  482. oext_alen - end_ext_alen);
  483. }
  484. ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
  485. o_end, &start_ext, &new_ext, &end_ext);
  486. return ret;
  487. }
  488. /**
  489. * mext_calc_swap_extents - Calculate extents for extent swapping.
  490. *
  491. * @tmp_dext: the extent that will belong to the original inode
  492. * @tmp_oext: the extent that will belong to the donor inode
  493. * @orig_off: block offset of original inode
  494. * @donor_off: block offset of donor inode
  495. * @max_count: the maximun length of extents
  496. */
  497. static void
  498. mext_calc_swap_extents(struct ext4_extent *tmp_dext,
  499. struct ext4_extent *tmp_oext,
  500. ext4_lblk_t orig_off, ext4_lblk_t donor_off,
  501. ext4_lblk_t max_count)
  502. {
  503. ext4_lblk_t diff, orig_diff;
  504. struct ext4_extent dext_old, oext_old;
  505. dext_old = *tmp_dext;
  506. oext_old = *tmp_oext;
  507. /* When tmp_dext is too large, pick up the target range. */
  508. diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
  509. ext4_ext_store_pblock(tmp_dext, ext_pblock(tmp_dext) + diff);
  510. tmp_dext->ee_block =
  511. cpu_to_le32(le32_to_cpu(tmp_dext->ee_block) + diff);
  512. tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_dext->ee_len) - diff);
  513. if (max_count < ext4_ext_get_actual_len(tmp_dext))
  514. tmp_dext->ee_len = cpu_to_le16(max_count);
  515. orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
  516. ext4_ext_store_pblock(tmp_oext, ext_pblock(tmp_oext) + orig_diff);
  517. /* Adjust extent length if donor extent is larger than orig */
  518. if (ext4_ext_get_actual_len(tmp_dext) >
  519. ext4_ext_get_actual_len(tmp_oext) - orig_diff)
  520. tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
  521. orig_diff);
  522. tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
  523. copy_extent_status(&oext_old, tmp_dext);
  524. copy_extent_status(&dext_old, tmp_oext);
  525. }
  526. /**
  527. * mext_replace_branches - Replace original extents with new extents
  528. *
  529. * @handle: journal handle
  530. * @orig_inode: original inode
  531. * @donor_inode: donor inode
  532. * @from: block offset of orig_inode
  533. * @count: block count to be replaced
  534. *
  535. * Replace original inode extents and donor inode extents page by page.
  536. * We implement this replacement in the following three steps:
  537. * 1. Save the block information of original and donor inodes into
  538. * dummy extents.
  539. * 2. Change the block information of original inode to point at the
  540. * donor inode blocks.
  541. * 3. Change the block information of donor inode to point at the saved
  542. * original inode blocks in the dummy extents.
  543. *
  544. * Return 0 on success, or a negative error value on failure.
  545. */
  546. static int
  547. mext_replace_branches(handle_t *handle, struct inode *orig_inode,
  548. struct inode *donor_inode, ext4_lblk_t from,
  549. ext4_lblk_t count)
  550. {
  551. struct ext4_ext_path *orig_path = NULL;
  552. struct ext4_ext_path *donor_path = NULL;
  553. struct ext4_extent *oext, *dext;
  554. struct ext4_extent tmp_dext, tmp_oext;
  555. ext4_lblk_t orig_off = from, donor_off = from;
  556. int err = 0;
  557. int depth;
  558. int replaced_count = 0;
  559. int dext_alen;
  560. mext_double_down_write(orig_inode, donor_inode);
  561. /* Get the original extent for the block "orig_off" */
  562. get_ext_path(orig_path, orig_inode, orig_off, err);
  563. if (orig_path == NULL)
  564. goto out;
  565. /* Get the donor extent for the head */
  566. get_ext_path(donor_path, donor_inode, donor_off, err);
  567. if (donor_path == NULL)
  568. goto out;
  569. depth = ext_depth(orig_inode);
  570. oext = orig_path[depth].p_ext;
  571. tmp_oext = *oext;
  572. depth = ext_depth(donor_inode);
  573. dext = donor_path[depth].p_ext;
  574. tmp_dext = *dext;
  575. mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
  576. donor_off, count);
  577. /* Loop for the donor extents */
  578. while (1) {
  579. /* The extent for donor must be found. */
  580. BUG_ON(!dext || donor_off != le32_to_cpu(tmp_dext.ee_block));
  581. /* Set donor extent to orig extent */
  582. err = mext_leaf_block(handle, orig_inode,
  583. orig_path, &tmp_dext, &orig_off);
  584. if (err < 0)
  585. goto out;
  586. /* Set orig extent to donor extent */
  587. err = mext_leaf_block(handle, donor_inode,
  588. donor_path, &tmp_oext, &donor_off);
  589. if (err < 0)
  590. goto out;
  591. dext_alen = ext4_ext_get_actual_len(&tmp_dext);
  592. replaced_count += dext_alen;
  593. donor_off += dext_alen;
  594. orig_off += dext_alen;
  595. /* Already moved the expected blocks */
  596. if (replaced_count >= count)
  597. break;
  598. if (orig_path)
  599. ext4_ext_drop_refs(orig_path);
  600. get_ext_path(orig_path, orig_inode, orig_off, err);
  601. if (orig_path == NULL)
  602. goto out;
  603. depth = ext_depth(orig_inode);
  604. oext = orig_path[depth].p_ext;
  605. if (le32_to_cpu(oext->ee_block) +
  606. ext4_ext_get_actual_len(oext) <= orig_off) {
  607. err = 0;
  608. goto out;
  609. }
  610. tmp_oext = *oext;
  611. if (donor_path)
  612. ext4_ext_drop_refs(donor_path);
  613. get_ext_path(donor_path, donor_inode,
  614. donor_off, err);
  615. if (donor_path == NULL)
  616. goto out;
  617. depth = ext_depth(donor_inode);
  618. dext = donor_path[depth].p_ext;
  619. if (le32_to_cpu(dext->ee_block) +
  620. ext4_ext_get_actual_len(dext) <= donor_off) {
  621. err = 0;
  622. goto out;
  623. }
  624. tmp_dext = *dext;
  625. mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
  626. donor_off,
  627. count - replaced_count);
  628. }
  629. out:
  630. if (orig_path) {
  631. ext4_ext_drop_refs(orig_path);
  632. kfree(orig_path);
  633. }
  634. if (donor_path) {
  635. ext4_ext_drop_refs(donor_path);
  636. kfree(donor_path);
  637. }
  638. mext_double_up_write(orig_inode, donor_inode);
  639. return err;
  640. }
  641. /**
  642. * move_extent_per_page - Move extent data per page
  643. *
  644. * @o_filp: file structure of original file
  645. * @donor_inode: donor inode
  646. * @orig_page_offset: page index on original file
  647. * @data_offset_in_page: block index where data swapping starts
  648. * @block_len_in_page: the number of blocks to be swapped
  649. * @uninit: orig extent is uninitialized or not
  650. *
  651. * Save the data in original inode blocks and replace original inode extents
  652. * with donor inode extents by calling mext_replace_branches().
  653. * Finally, write out the saved data in new original inode blocks. Return 0
  654. * on success, or a negative error value on failure.
  655. */
  656. static int
  657. move_extent_par_page(struct file *o_filp, struct inode *donor_inode,
  658. pgoff_t orig_page_offset, int data_offset_in_page,
  659. int block_len_in_page, int uninit)
  660. {
  661. struct inode *orig_inode = o_filp->f_dentry->d_inode;
  662. struct address_space *mapping = orig_inode->i_mapping;
  663. struct buffer_head *bh;
  664. struct page *page = NULL;
  665. const struct address_space_operations *a_ops = mapping->a_ops;
  666. handle_t *handle;
  667. ext4_lblk_t orig_blk_offset;
  668. long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
  669. unsigned long blocksize = orig_inode->i_sb->s_blocksize;
  670. unsigned int w_flags = 0;
  671. unsigned int tmp_data_len, data_len;
  672. void *fsdata;
  673. int ret, i, jblocks;
  674. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  675. /*
  676. * It needs twice the amount of ordinary journal buffers because
  677. * inode and donor_inode may change each different metadata blocks.
  678. */
  679. jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
  680. handle = ext4_journal_start(orig_inode, jblocks);
  681. if (IS_ERR(handle)) {
  682. ret = PTR_ERR(handle);
  683. return ret;
  684. }
  685. if (segment_eq(get_fs(), KERNEL_DS))
  686. w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
  687. orig_blk_offset = orig_page_offset * blocks_per_page +
  688. data_offset_in_page;
  689. /*
  690. * If orig extent is uninitialized one,
  691. * it's not necessary force the page into memory
  692. * and then force it to be written out again.
  693. * Just swap data blocks between orig and donor.
  694. */
  695. if (uninit) {
  696. ret = mext_replace_branches(handle, orig_inode,
  697. donor_inode, orig_blk_offset,
  698. block_len_in_page);
  699. /* Clear the inode cache not to refer to the old data */
  700. ext4_ext_invalidate_cache(orig_inode);
  701. ext4_ext_invalidate_cache(donor_inode);
  702. goto out2;
  703. }
  704. offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
  705. /* Calculate data_len */
  706. if ((orig_blk_offset + block_len_in_page - 1) ==
  707. ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
  708. /* Replace the last block */
  709. tmp_data_len = orig_inode->i_size & (blocksize - 1);
  710. /*
  711. * If data_len equal zero, it shows data_len is multiples of
  712. * blocksize. So we set appropriate value.
  713. */
  714. if (tmp_data_len == 0)
  715. tmp_data_len = blocksize;
  716. data_len = tmp_data_len +
  717. ((block_len_in_page - 1) << orig_inode->i_blkbits);
  718. } else {
  719. data_len = block_len_in_page << orig_inode->i_blkbits;
  720. }
  721. ret = a_ops->write_begin(o_filp, mapping, offs, data_len, w_flags,
  722. &page, &fsdata);
  723. if (unlikely(ret < 0))
  724. goto out;
  725. if (!PageUptodate(page)) {
  726. mapping->a_ops->readpage(o_filp, page);
  727. lock_page(page);
  728. }
  729. /*
  730. * try_to_release_page() doesn't call releasepage in writeback mode.
  731. * We should care about the order of writing to the same file
  732. * by multiple move extent processes.
  733. * It needs to call wait_on_page_writeback() to wait for the
  734. * writeback of the page.
  735. */
  736. if (PageWriteback(page))
  737. wait_on_page_writeback(page);
  738. /* Release old bh and drop refs */
  739. try_to_release_page(page, 0);
  740. ret = mext_replace_branches(handle, orig_inode, donor_inode,
  741. orig_blk_offset, block_len_in_page);
  742. if (ret < 0)
  743. goto out;
  744. /* Clear the inode cache not to refer to the old data */
  745. ext4_ext_invalidate_cache(orig_inode);
  746. ext4_ext_invalidate_cache(donor_inode);
  747. if (!page_has_buffers(page))
  748. create_empty_buffers(page, 1 << orig_inode->i_blkbits, 0);
  749. bh = page_buffers(page);
  750. for (i = 0; i < data_offset_in_page; i++)
  751. bh = bh->b_this_page;
  752. for (i = 0; i < block_len_in_page; i++) {
  753. ret = ext4_get_block(orig_inode,
  754. (sector_t)(orig_blk_offset + i), bh, 0);
  755. if (ret < 0)
  756. goto out;
  757. if (bh->b_this_page != NULL)
  758. bh = bh->b_this_page;
  759. }
  760. ret = a_ops->write_end(o_filp, mapping, offs, data_len, data_len,
  761. page, fsdata);
  762. page = NULL;
  763. out:
  764. if (unlikely(page)) {
  765. if (PageLocked(page))
  766. unlock_page(page);
  767. page_cache_release(page);
  768. ext4_journal_stop(handle);
  769. }
  770. out2:
  771. ext4_journal_stop(handle);
  772. return ret < 0 ? ret : 0;
  773. }
  774. /**
  775. * mext_check_argumants - Check whether move extent can be done
  776. *
  777. * @orig_inode: original inode
  778. * @donor_inode: donor inode
  779. * @orig_start: logical start offset in block for orig
  780. * @donor_start: logical start offset in block for donor
  781. * @len: the number of blocks to be moved
  782. * @moved_len: moved block length
  783. *
  784. * Check the arguments of ext4_move_extents() whether the files can be
  785. * exchanged with each other.
  786. * Return 0 on success, or a negative error value on failure.
  787. */
  788. static int
  789. mext_check_arguments(struct inode *orig_inode,
  790. struct inode *donor_inode, __u64 orig_start,
  791. __u64 donor_start, __u64 *len, __u64 moved_len)
  792. {
  793. /* Regular file check */
  794. if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
  795. ext4_debug("ext4 move extent: The argument files should be "
  796. "regular file [ino:orig %lu, donor %lu]\n",
  797. orig_inode->i_ino, donor_inode->i_ino);
  798. return -EINVAL;
  799. }
  800. /* Ext4 move extent does not support swapfile */
  801. if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
  802. ext4_debug("ext4 move extent: The argument files should "
  803. "not be swapfile [ino:orig %lu, donor %lu]\n",
  804. orig_inode->i_ino, donor_inode->i_ino);
  805. return -EINVAL;
  806. }
  807. /* Files should be in the same ext4 FS */
  808. if (orig_inode->i_sb != donor_inode->i_sb) {
  809. ext4_debug("ext4 move extent: The argument files "
  810. "should be in same FS [ino:orig %lu, donor %lu]\n",
  811. orig_inode->i_ino, donor_inode->i_ino);
  812. return -EINVAL;
  813. }
  814. /* orig and donor should be different file */
  815. if (orig_inode->i_ino == donor_inode->i_ino) {
  816. ext4_debug("ext4 move extent: The argument files should not "
  817. "be same file [ino:orig %lu, donor %lu]\n",
  818. orig_inode->i_ino, donor_inode->i_ino);
  819. return -EINVAL;
  820. }
  821. /* Ext4 move extent supports only extent based file */
  822. if (!(EXT4_I(orig_inode)->i_flags & EXT4_EXTENTS_FL)) {
  823. ext4_debug("ext4 move extent: orig file is not extents "
  824. "based file [ino:orig %lu]\n", orig_inode->i_ino);
  825. return -EOPNOTSUPP;
  826. } else if (!(EXT4_I(donor_inode)->i_flags & EXT4_EXTENTS_FL)) {
  827. ext4_debug("ext4 move extent: donor file is not extents "
  828. "based file [ino:donor %lu]\n", donor_inode->i_ino);
  829. return -EOPNOTSUPP;
  830. }
  831. if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
  832. ext4_debug("ext4 move extent: File size is 0 byte\n");
  833. return -EINVAL;
  834. }
  835. /* Start offset should be same */
  836. if (orig_start != donor_start) {
  837. ext4_debug("ext4 move extent: orig and donor's start "
  838. "offset are not same [ino:orig %lu, donor %lu]\n",
  839. orig_inode->i_ino, donor_inode->i_ino);
  840. return -EINVAL;
  841. }
  842. if (moved_len) {
  843. ext4_debug("ext4 move extent: moved_len should be 0 "
  844. "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
  845. donor_inode->i_ino);
  846. return -EINVAL;
  847. }
  848. if ((orig_start > MAX_DEFRAG_SIZE) ||
  849. (donor_start > MAX_DEFRAG_SIZE) ||
  850. (*len > MAX_DEFRAG_SIZE) ||
  851. (orig_start + *len > MAX_DEFRAG_SIZE)) {
  852. ext4_debug("ext4 move extent: Can't handle over [%lu] blocks "
  853. "[ino:orig %lu, donor %lu]\n", MAX_DEFRAG_SIZE,
  854. orig_inode->i_ino, donor_inode->i_ino);
  855. return -EINVAL;
  856. }
  857. if (orig_inode->i_size > donor_inode->i_size) {
  858. if (orig_start >= donor_inode->i_size) {
  859. ext4_debug("ext4 move extent: orig start offset "
  860. "[%llu] should be less than donor file size "
  861. "[%lld] [ino:orig %lu, donor_inode %lu]\n",
  862. orig_start, donor_inode->i_size,
  863. orig_inode->i_ino, donor_inode->i_ino);
  864. return -EINVAL;
  865. }
  866. if (orig_start + *len > donor_inode->i_size) {
  867. ext4_debug("ext4 move extent: End offset [%llu] should "
  868. "be less than donor file size [%lld]."
  869. "So adjust length from %llu to %lld "
  870. "[ino:orig %lu, donor %lu]\n",
  871. orig_start + *len, donor_inode->i_size,
  872. *len, donor_inode->i_size - orig_start,
  873. orig_inode->i_ino, donor_inode->i_ino);
  874. *len = donor_inode->i_size - orig_start;
  875. }
  876. } else {
  877. if (orig_start >= orig_inode->i_size) {
  878. ext4_debug("ext4 move extent: start offset [%llu] "
  879. "should be less than original file size "
  880. "[%lld] [inode:orig %lu, donor %lu]\n",
  881. orig_start, orig_inode->i_size,
  882. orig_inode->i_ino, donor_inode->i_ino);
  883. return -EINVAL;
  884. }
  885. if (orig_start + *len > orig_inode->i_size) {
  886. ext4_debug("ext4 move extent: Adjust length "
  887. "from %llu to %lld. Because it should be "
  888. "less than original file size "
  889. "[ino:orig %lu, donor %lu]\n",
  890. *len, orig_inode->i_size - orig_start,
  891. orig_inode->i_ino, donor_inode->i_ino);
  892. *len = orig_inode->i_size - orig_start;
  893. }
  894. }
  895. if (!*len) {
  896. ext4_debug("ext4 move extent: len shoudld not be 0 "
  897. "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
  898. donor_inode->i_ino);
  899. return -EINVAL;
  900. }
  901. return 0;
  902. }
  903. /**
  904. * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
  905. *
  906. * @inode1: the inode structure
  907. * @inode2: the inode structure
  908. *
  909. * Lock two inodes' i_mutex by i_ino order. This function is moved from
  910. * fs/inode.c.
  911. */
  912. static void
  913. mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
  914. {
  915. if (inode1 == NULL || inode2 == NULL || inode1 == inode2) {
  916. if (inode1)
  917. mutex_lock(&inode1->i_mutex);
  918. else if (inode2)
  919. mutex_lock(&inode2->i_mutex);
  920. return;
  921. }
  922. if (inode1->i_ino < inode2->i_ino) {
  923. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  924. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  925. } else {
  926. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
  927. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
  928. }
  929. }
  930. /**
  931. * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
  932. *
  933. * @inode1: the inode that is released first
  934. * @inode2: the inode that is released second
  935. *
  936. * This function is moved from fs/inode.c.
  937. */
  938. static void
  939. mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
  940. {
  941. if (inode1)
  942. mutex_unlock(&inode1->i_mutex);
  943. if (inode2 && inode2 != inode1)
  944. mutex_unlock(&inode2->i_mutex);
  945. }
  946. /**
  947. * ext4_move_extents - Exchange the specified range of a file
  948. *
  949. * @o_filp: file structure of the original file
  950. * @d_filp: file structure of the donor file
  951. * @orig_start: start offset in block for orig
  952. * @donor_start: start offset in block for donor
  953. * @len: the number of blocks to be moved
  954. * @moved_len: moved block length
  955. *
  956. * This function returns 0 and moved block length is set in moved_len
  957. * if succeed, otherwise returns error value.
  958. *
  959. * Note: ext4_move_extents() proceeds the following order.
  960. * 1:ext4_move_extents() calculates the last block number of moving extent
  961. * function by the start block number (orig_start) and the number of blocks
  962. * to be moved (len) specified as arguments.
  963. * If the {orig, donor}_start points a hole, the extent's start offset
  964. * pointed by ext_cur (current extent), holecheck_path, orig_path are set
  965. * after hole behind.
  966. * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
  967. * or the ext_cur exceeds the block_end which is last logical block number.
  968. * 3:To get the length of continues area, call mext_next_extent()
  969. * specified with the ext_cur (initial value is holecheck_path) re-cursive,
  970. * until find un-continuous extent, the start logical block number exceeds
  971. * the block_end or the extent points to the last extent.
  972. * 4:Exchange the original inode data with donor inode data
  973. * from orig_page_offset to seq_end_page.
  974. * The start indexes of data are specified as arguments.
  975. * That of the original inode is orig_page_offset,
  976. * and the donor inode is also orig_page_offset
  977. * (To easily handle blocksize != pagesize case, the offset for the
  978. * donor inode is block unit).
  979. * 5:Update holecheck_path and orig_path to points a next proceeding extent,
  980. * then returns to step 2.
  981. * 6:Release holecheck_path, orig_path and set the len to moved_len
  982. * which shows the number of moved blocks.
  983. * The moved_len is useful for the command to calculate the file offset
  984. * for starting next move extent ioctl.
  985. * 7:Return 0 on success, or a negative error value on failure.
  986. */
  987. int
  988. ext4_move_extents(struct file *o_filp, struct file *d_filp,
  989. __u64 orig_start, __u64 donor_start, __u64 len,
  990. __u64 *moved_len)
  991. {
  992. struct inode *orig_inode = o_filp->f_dentry->d_inode;
  993. struct inode *donor_inode = d_filp->f_dentry->d_inode;
  994. struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
  995. struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
  996. ext4_lblk_t block_start = orig_start;
  997. ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
  998. ext4_lblk_t rest_blocks;
  999. pgoff_t orig_page_offset = 0, seq_end_page;
  1000. int ret, depth, last_extent = 0;
  1001. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  1002. int data_offset_in_page;
  1003. int block_len_in_page;
  1004. int uninit;
  1005. /* protect orig and donor against a truncate */
  1006. mext_inode_double_lock(orig_inode, donor_inode);
  1007. mext_double_down_read(orig_inode, donor_inode);
  1008. /* Check the filesystem environment whether move_extent can be done */
  1009. ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
  1010. donor_start, &len, *moved_len);
  1011. mext_double_up_read(orig_inode, donor_inode);
  1012. if (ret)
  1013. goto out2;
  1014. file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
  1015. block_end = block_start + len - 1;
  1016. if (file_end < block_end)
  1017. len -= block_end - file_end;
  1018. get_ext_path(orig_path, orig_inode, block_start, ret);
  1019. if (orig_path == NULL)
  1020. goto out2;
  1021. /* Get path structure to check the hole */
  1022. get_ext_path(holecheck_path, orig_inode, block_start, ret);
  1023. if (holecheck_path == NULL)
  1024. goto out;
  1025. depth = ext_depth(orig_inode);
  1026. ext_cur = holecheck_path[depth].p_ext;
  1027. if (ext_cur == NULL) {
  1028. ret = -EINVAL;
  1029. goto out;
  1030. }
  1031. /*
  1032. * Get proper extent whose ee_block is beyond block_start
  1033. * if block_start was within the hole.
  1034. */
  1035. if (le32_to_cpu(ext_cur->ee_block) +
  1036. ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
  1037. last_extent = mext_next_extent(orig_inode,
  1038. holecheck_path, &ext_cur);
  1039. if (last_extent < 0) {
  1040. ret = last_extent;
  1041. goto out;
  1042. }
  1043. last_extent = mext_next_extent(orig_inode, orig_path,
  1044. &ext_dummy);
  1045. if (last_extent < 0) {
  1046. ret = last_extent;
  1047. goto out;
  1048. }
  1049. }
  1050. seq_start = block_start;
  1051. /* No blocks within the specified range. */
  1052. if (le32_to_cpu(ext_cur->ee_block) > block_end) {
  1053. ext4_debug("ext4 move extent: The specified range of file "
  1054. "may be the hole\n");
  1055. ret = -EINVAL;
  1056. goto out;
  1057. }
  1058. /* Adjust start blocks */
  1059. add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
  1060. ext4_ext_get_actual_len(ext_cur), block_end + 1) -
  1061. max(le32_to_cpu(ext_cur->ee_block), block_start);
  1062. while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
  1063. seq_blocks += add_blocks;
  1064. /* Adjust tail blocks */
  1065. if (seq_start + seq_blocks - 1 > block_end)
  1066. seq_blocks = block_end - seq_start + 1;
  1067. ext_prev = ext_cur;
  1068. last_extent = mext_next_extent(orig_inode, holecheck_path,
  1069. &ext_cur);
  1070. if (last_extent < 0) {
  1071. ret = last_extent;
  1072. break;
  1073. }
  1074. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1075. /*
  1076. * Extend the length of contiguous block (seq_blocks)
  1077. * if extents are contiguous.
  1078. */
  1079. if (ext4_can_extents_be_merged(orig_inode,
  1080. ext_prev, ext_cur) &&
  1081. block_end >= le32_to_cpu(ext_cur->ee_block) &&
  1082. !last_extent)
  1083. continue;
  1084. /* Is original extent is uninitialized */
  1085. uninit = ext4_ext_is_uninitialized(ext_prev);
  1086. data_offset_in_page = seq_start % blocks_per_page;
  1087. /*
  1088. * Calculate data blocks count that should be swapped
  1089. * at the first page.
  1090. */
  1091. if (data_offset_in_page + seq_blocks > blocks_per_page) {
  1092. /* Swapped blocks are across pages */
  1093. block_len_in_page =
  1094. blocks_per_page - data_offset_in_page;
  1095. } else {
  1096. /* Swapped blocks are in a page */
  1097. block_len_in_page = seq_blocks;
  1098. }
  1099. orig_page_offset = seq_start >>
  1100. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1101. seq_end_page = (seq_start + seq_blocks - 1) >>
  1102. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1103. seq_start = le32_to_cpu(ext_cur->ee_block);
  1104. rest_blocks = seq_blocks;
  1105. /* Discard preallocations of two inodes */
  1106. down_write(&EXT4_I(orig_inode)->i_data_sem);
  1107. ext4_discard_preallocations(orig_inode);
  1108. up_write(&EXT4_I(orig_inode)->i_data_sem);
  1109. down_write(&EXT4_I(donor_inode)->i_data_sem);
  1110. ext4_discard_preallocations(donor_inode);
  1111. up_write(&EXT4_I(donor_inode)->i_data_sem);
  1112. while (orig_page_offset <= seq_end_page) {
  1113. /* Swap original branches with new branches */
  1114. ret = move_extent_par_page(o_filp, donor_inode,
  1115. orig_page_offset,
  1116. data_offset_in_page,
  1117. block_len_in_page, uninit);
  1118. if (ret < 0)
  1119. goto out;
  1120. orig_page_offset++;
  1121. /* Count how many blocks we have exchanged */
  1122. *moved_len += block_len_in_page;
  1123. BUG_ON(*moved_len > len);
  1124. data_offset_in_page = 0;
  1125. rest_blocks -= block_len_in_page;
  1126. if (rest_blocks > blocks_per_page)
  1127. block_len_in_page = blocks_per_page;
  1128. else
  1129. block_len_in_page = rest_blocks;
  1130. }
  1131. /* Decrease buffer counter */
  1132. if (holecheck_path)
  1133. ext4_ext_drop_refs(holecheck_path);
  1134. get_ext_path(holecheck_path, orig_inode,
  1135. seq_start, ret);
  1136. if (holecheck_path == NULL)
  1137. break;
  1138. depth = holecheck_path->p_depth;
  1139. /* Decrease buffer counter */
  1140. if (orig_path)
  1141. ext4_ext_drop_refs(orig_path);
  1142. get_ext_path(orig_path, orig_inode, seq_start, ret);
  1143. if (orig_path == NULL)
  1144. break;
  1145. ext_cur = holecheck_path[depth].p_ext;
  1146. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1147. seq_blocks = 0;
  1148. }
  1149. out:
  1150. if (orig_path) {
  1151. ext4_ext_drop_refs(orig_path);
  1152. kfree(orig_path);
  1153. }
  1154. if (holecheck_path) {
  1155. ext4_ext_drop_refs(holecheck_path);
  1156. kfree(holecheck_path);
  1157. }
  1158. out2:
  1159. mext_inode_double_unlock(orig_inode, donor_inode);
  1160. if (ret)
  1161. return ret;
  1162. /* All of the specified blocks must be exchanged in succeed */
  1163. BUG_ON(*moved_len != len);
  1164. return 0;
  1165. }