move_extent.c 41 KB

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