move_extent.c 40 KB

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