move_extent.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. /* Protect extent trees against block allocations via delalloc */
  591. double_down_write_data_sem(orig_inode, donor_inode);
  592. /* Get the original extent for the block "orig_off" */
  593. *err = get_ext_path(orig_inode, orig_off, &orig_path);
  594. if (*err)
  595. goto out;
  596. /* Get the donor extent for the head */
  597. *err = get_ext_path(donor_inode, donor_off, &donor_path);
  598. if (*err)
  599. goto out;
  600. depth = ext_depth(orig_inode);
  601. oext = orig_path[depth].p_ext;
  602. tmp_oext = *oext;
  603. depth = ext_depth(donor_inode);
  604. dext = donor_path[depth].p_ext;
  605. tmp_dext = *dext;
  606. *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
  607. donor_off, count);
  608. if (*err)
  609. goto out;
  610. /* Loop for the donor extents */
  611. while (1) {
  612. /* The extent for donor must be found. */
  613. if (!dext) {
  614. ext4_error(donor_inode->i_sb, __func__,
  615. "The extent for donor must be found");
  616. *err = -EIO;
  617. goto out;
  618. } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
  619. ext4_error(donor_inode->i_sb, __func__,
  620. "Donor offset(%u) and the first block of donor "
  621. "extent(%u) should be equal",
  622. donor_off,
  623. le32_to_cpu(tmp_dext.ee_block));
  624. *err = -EIO;
  625. goto out;
  626. }
  627. /* Set donor extent to orig extent */
  628. *err = mext_leaf_block(handle, orig_inode,
  629. orig_path, &tmp_dext, &orig_off);
  630. if (*err)
  631. goto out;
  632. /* Set orig extent to donor extent */
  633. *err = mext_leaf_block(handle, donor_inode,
  634. donor_path, &tmp_oext, &donor_off);
  635. if (*err)
  636. goto out;
  637. dext_alen = ext4_ext_get_actual_len(&tmp_dext);
  638. replaced_count += dext_alen;
  639. donor_off += dext_alen;
  640. orig_off += dext_alen;
  641. /* Already moved the expected blocks */
  642. if (replaced_count >= count)
  643. break;
  644. if (orig_path)
  645. ext4_ext_drop_refs(orig_path);
  646. *err = get_ext_path(orig_inode, orig_off, &orig_path);
  647. if (*err)
  648. goto out;
  649. depth = ext_depth(orig_inode);
  650. oext = orig_path[depth].p_ext;
  651. tmp_oext = *oext;
  652. if (donor_path)
  653. ext4_ext_drop_refs(donor_path);
  654. *err = get_ext_path(donor_inode, donor_off, &donor_path);
  655. if (*err)
  656. goto out;
  657. depth = ext_depth(donor_inode);
  658. dext = donor_path[depth].p_ext;
  659. tmp_dext = *dext;
  660. *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
  661. donor_off, count - replaced_count);
  662. if (*err)
  663. goto out;
  664. }
  665. out:
  666. if (orig_path) {
  667. ext4_ext_drop_refs(orig_path);
  668. kfree(orig_path);
  669. }
  670. if (donor_path) {
  671. ext4_ext_drop_refs(donor_path);
  672. kfree(donor_path);
  673. }
  674. ext4_ext_invalidate_cache(orig_inode);
  675. ext4_ext_invalidate_cache(donor_inode);
  676. double_up_write_data_sem(orig_inode, donor_inode);
  677. return replaced_count;
  678. }
  679. /**
  680. * move_extent_per_page - Move extent data per page
  681. *
  682. * @o_filp: file structure of original file
  683. * @donor_inode: donor inode
  684. * @orig_page_offset: page index on original file
  685. * @data_offset_in_page: block index where data swapping starts
  686. * @block_len_in_page: the number of blocks to be swapped
  687. * @uninit: orig extent is uninitialized or not
  688. * @err: pointer to save return value
  689. *
  690. * Save the data in original inode blocks and replace original inode extents
  691. * with donor inode extents by calling mext_replace_branches().
  692. * Finally, write out the saved data in new original inode blocks. Return
  693. * replaced block count.
  694. */
  695. static int
  696. move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
  697. pgoff_t orig_page_offset, int data_offset_in_page,
  698. int block_len_in_page, int uninit, int *err)
  699. {
  700. struct inode *orig_inode = o_filp->f_dentry->d_inode;
  701. struct address_space *mapping = orig_inode->i_mapping;
  702. struct buffer_head *bh;
  703. struct page *page = NULL;
  704. const struct address_space_operations *a_ops = mapping->a_ops;
  705. handle_t *handle;
  706. ext4_lblk_t orig_blk_offset;
  707. long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
  708. unsigned long blocksize = orig_inode->i_sb->s_blocksize;
  709. unsigned int w_flags = 0;
  710. unsigned int tmp_data_size, data_size, replaced_size;
  711. void *fsdata;
  712. int i, jblocks;
  713. int err2 = 0;
  714. int replaced_count = 0;
  715. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  716. /*
  717. * It needs twice the amount of ordinary journal buffers because
  718. * inode and donor_inode may change each different metadata blocks.
  719. */
  720. jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
  721. handle = ext4_journal_start(orig_inode, jblocks);
  722. if (IS_ERR(handle)) {
  723. *err = PTR_ERR(handle);
  724. return 0;
  725. }
  726. if (segment_eq(get_fs(), KERNEL_DS))
  727. w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
  728. orig_blk_offset = orig_page_offset * blocks_per_page +
  729. data_offset_in_page;
  730. /*
  731. * If orig extent is uninitialized one,
  732. * it's not necessary force the page into memory
  733. * and then force it to be written out again.
  734. * Just swap data blocks between orig and donor.
  735. */
  736. if (uninit) {
  737. replaced_count = mext_replace_branches(handle, orig_inode,
  738. donor_inode, orig_blk_offset,
  739. block_len_in_page, err);
  740. goto out2;
  741. }
  742. offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
  743. /* Calculate data_size */
  744. if ((orig_blk_offset + block_len_in_page - 1) ==
  745. ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
  746. /* Replace the last block */
  747. tmp_data_size = orig_inode->i_size & (blocksize - 1);
  748. /*
  749. * If data_size equal zero, it shows data_size is multiples of
  750. * blocksize. So we set appropriate value.
  751. */
  752. if (tmp_data_size == 0)
  753. tmp_data_size = blocksize;
  754. data_size = tmp_data_size +
  755. ((block_len_in_page - 1) << orig_inode->i_blkbits);
  756. } else
  757. data_size = block_len_in_page << orig_inode->i_blkbits;
  758. replaced_size = data_size;
  759. *err = a_ops->write_begin(o_filp, mapping, offs, data_size, w_flags,
  760. &page, &fsdata);
  761. if (unlikely(*err < 0))
  762. goto out;
  763. if (!PageUptodate(page)) {
  764. mapping->a_ops->readpage(o_filp, page);
  765. lock_page(page);
  766. }
  767. /*
  768. * try_to_release_page() doesn't call releasepage in writeback mode.
  769. * We should care about the order of writing to the same file
  770. * by multiple move extent processes.
  771. * It needs to call wait_on_page_writeback() to wait for the
  772. * writeback of the page.
  773. */
  774. if (PageWriteback(page))
  775. wait_on_page_writeback(page);
  776. /* Release old bh and drop refs */
  777. try_to_release_page(page, 0);
  778. replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
  779. orig_blk_offset, block_len_in_page,
  780. &err2);
  781. if (err2) {
  782. if (replaced_count) {
  783. block_len_in_page = replaced_count;
  784. replaced_size =
  785. block_len_in_page << orig_inode->i_blkbits;
  786. } else
  787. goto out;
  788. }
  789. if (!page_has_buffers(page))
  790. create_empty_buffers(page, 1 << orig_inode->i_blkbits, 0);
  791. bh = page_buffers(page);
  792. for (i = 0; i < data_offset_in_page; i++)
  793. bh = bh->b_this_page;
  794. for (i = 0; i < block_len_in_page; i++) {
  795. *err = ext4_get_block(orig_inode,
  796. (sector_t)(orig_blk_offset + i), bh, 0);
  797. if (*err < 0)
  798. goto out;
  799. if (bh->b_this_page != NULL)
  800. bh = bh->b_this_page;
  801. }
  802. *err = a_ops->write_end(o_filp, mapping, offs, data_size, replaced_size,
  803. page, fsdata);
  804. page = NULL;
  805. out:
  806. if (unlikely(page)) {
  807. if (PageLocked(page))
  808. unlock_page(page);
  809. page_cache_release(page);
  810. ext4_journal_stop(handle);
  811. }
  812. out2:
  813. ext4_journal_stop(handle);
  814. if (err2)
  815. *err = err2;
  816. return replaced_count;
  817. }
  818. /**
  819. * mext_check_argumants - Check whether move extent can be done
  820. *
  821. * @orig_inode: original inode
  822. * @donor_inode: donor inode
  823. * @orig_start: logical start offset in block for orig
  824. * @donor_start: logical start offset in block for donor
  825. * @len: the number of blocks to be moved
  826. *
  827. * Check the arguments of ext4_move_extents() whether the files can be
  828. * exchanged with each other.
  829. * Return 0 on success, or a negative error value on failure.
  830. */
  831. static int
  832. mext_check_arguments(struct inode *orig_inode,
  833. struct inode *donor_inode, __u64 orig_start,
  834. __u64 donor_start, __u64 *len)
  835. {
  836. ext4_lblk_t orig_blocks, donor_blocks;
  837. unsigned int blkbits = orig_inode->i_blkbits;
  838. unsigned int blocksize = 1 << blkbits;
  839. /* Regular file check */
  840. if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
  841. ext4_debug("ext4 move extent: The argument files should be "
  842. "regular file [ino:orig %lu, donor %lu]\n",
  843. orig_inode->i_ino, donor_inode->i_ino);
  844. return -EINVAL;
  845. }
  846. /* Ext4 move extent does not support swapfile */
  847. if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
  848. ext4_debug("ext4 move extent: The argument files should "
  849. "not be swapfile [ino:orig %lu, donor %lu]\n",
  850. orig_inode->i_ino, donor_inode->i_ino);
  851. return -EINVAL;
  852. }
  853. /* Files should be in the same ext4 FS */
  854. if (orig_inode->i_sb != donor_inode->i_sb) {
  855. ext4_debug("ext4 move extent: The argument files "
  856. "should be in same FS [ino:orig %lu, donor %lu]\n",
  857. orig_inode->i_ino, donor_inode->i_ino);
  858. return -EINVAL;
  859. }
  860. /* Ext4 move extent supports only extent based file */
  861. if (!(EXT4_I(orig_inode)->i_flags & EXT4_EXTENTS_FL)) {
  862. ext4_debug("ext4 move extent: orig file is not extents "
  863. "based file [ino:orig %lu]\n", orig_inode->i_ino);
  864. return -EOPNOTSUPP;
  865. } else if (!(EXT4_I(donor_inode)->i_flags & EXT4_EXTENTS_FL)) {
  866. ext4_debug("ext4 move extent: donor file is not extents "
  867. "based file [ino:donor %lu]\n", donor_inode->i_ino);
  868. return -EOPNOTSUPP;
  869. }
  870. if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
  871. ext4_debug("ext4 move extent: File size is 0 byte\n");
  872. return -EINVAL;
  873. }
  874. /* Start offset should be same */
  875. if (orig_start != donor_start) {
  876. ext4_debug("ext4 move extent: orig and donor's start "
  877. "offset are not same [ino:orig %lu, donor %lu]\n",
  878. orig_inode->i_ino, donor_inode->i_ino);
  879. return -EINVAL;
  880. }
  881. if ((orig_start > EXT_MAX_BLOCK) ||
  882. (donor_start > EXT_MAX_BLOCK) ||
  883. (*len > EXT_MAX_BLOCK) ||
  884. (orig_start + *len > EXT_MAX_BLOCK)) {
  885. ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
  886. "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCK,
  887. orig_inode->i_ino, donor_inode->i_ino);
  888. return -EINVAL;
  889. }
  890. if (orig_inode->i_size > donor_inode->i_size) {
  891. donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
  892. /* TODO: eliminate this artificial restriction */
  893. if (orig_start >= donor_blocks) {
  894. ext4_debug("ext4 move extent: orig start offset "
  895. "[%llu] should be less than donor file blocks "
  896. "[%u] [ino:orig %lu, donor %lu]\n",
  897. orig_start, donor_blocks,
  898. orig_inode->i_ino, donor_inode->i_ino);
  899. return -EINVAL;
  900. }
  901. /* TODO: eliminate this artificial restriction */
  902. if (orig_start + *len > donor_blocks) {
  903. ext4_debug("ext4 move extent: End offset [%llu] should "
  904. "be less than donor file blocks [%u]."
  905. "So adjust length from %llu to %llu "
  906. "[ino:orig %lu, donor %lu]\n",
  907. orig_start + *len, donor_blocks,
  908. *len, donor_blocks - orig_start,
  909. orig_inode->i_ino, donor_inode->i_ino);
  910. *len = donor_blocks - orig_start;
  911. }
  912. } else {
  913. orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
  914. if (orig_start >= orig_blocks) {
  915. ext4_debug("ext4 move extent: start offset [%llu] "
  916. "should be less than original file blocks "
  917. "[%u] [ino:orig %lu, donor %lu]\n",
  918. orig_start, orig_blocks,
  919. orig_inode->i_ino, donor_inode->i_ino);
  920. return -EINVAL;
  921. }
  922. if (orig_start + *len > orig_blocks) {
  923. ext4_debug("ext4 move extent: Adjust length "
  924. "from %llu to %llu. Because it should be "
  925. "less than original file blocks "
  926. "[ino:orig %lu, donor %lu]\n",
  927. *len, orig_blocks - orig_start,
  928. orig_inode->i_ino, donor_inode->i_ino);
  929. *len = orig_blocks - orig_start;
  930. }
  931. }
  932. if (!*len) {
  933. ext4_debug("ext4 move extent: len should not be 0 "
  934. "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
  935. donor_inode->i_ino);
  936. return -EINVAL;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
  942. *
  943. * @inode1: the inode structure
  944. * @inode2: the inode structure
  945. *
  946. * Lock two inodes' i_mutex by i_ino order.
  947. * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
  948. */
  949. static int
  950. mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
  951. {
  952. int ret = 0;
  953. BUG_ON(inode1 == NULL && inode2 == NULL);
  954. ret = mext_check_null_inode(inode1, inode2, __func__);
  955. if (ret < 0)
  956. goto out;
  957. if (inode1 == inode2) {
  958. mutex_lock(&inode1->i_mutex);
  959. goto out;
  960. }
  961. if (inode1->i_ino < inode2->i_ino) {
  962. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  963. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  964. } else {
  965. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
  966. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
  967. }
  968. out:
  969. return ret;
  970. }
  971. /**
  972. * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
  973. *
  974. * @inode1: the inode that is released first
  975. * @inode2: the inode that is released second
  976. *
  977. * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
  978. */
  979. static int
  980. mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
  981. {
  982. int ret = 0;
  983. BUG_ON(inode1 == NULL && inode2 == NULL);
  984. ret = mext_check_null_inode(inode1, inode2, __func__);
  985. if (ret < 0)
  986. goto out;
  987. if (inode1)
  988. mutex_unlock(&inode1->i_mutex);
  989. if (inode2 && inode2 != inode1)
  990. mutex_unlock(&inode2->i_mutex);
  991. out:
  992. return ret;
  993. }
  994. /**
  995. * ext4_move_extents - Exchange the specified range of a file
  996. *
  997. * @o_filp: file structure of the original file
  998. * @d_filp: file structure of the donor file
  999. * @orig_start: start offset in block for orig
  1000. * @donor_start: start offset in block for donor
  1001. * @len: the number of blocks to be moved
  1002. * @moved_len: moved block length
  1003. *
  1004. * This function returns 0 and moved block length is set in moved_len
  1005. * if succeed, otherwise returns error value.
  1006. *
  1007. * Note: ext4_move_extents() proceeds the following order.
  1008. * 1:ext4_move_extents() calculates the last block number of moving extent
  1009. * function by the start block number (orig_start) and the number of blocks
  1010. * to be moved (len) specified as arguments.
  1011. * If the {orig, donor}_start points a hole, the extent's start offset
  1012. * pointed by ext_cur (current extent), holecheck_path, orig_path are set
  1013. * after hole behind.
  1014. * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
  1015. * or the ext_cur exceeds the block_end which is last logical block number.
  1016. * 3:To get the length of continues area, call mext_next_extent()
  1017. * specified with the ext_cur (initial value is holecheck_path) re-cursive,
  1018. * until find un-continuous extent, the start logical block number exceeds
  1019. * the block_end or the extent points to the last extent.
  1020. * 4:Exchange the original inode data with donor inode data
  1021. * from orig_page_offset to seq_end_page.
  1022. * The start indexes of data are specified as arguments.
  1023. * That of the original inode is orig_page_offset,
  1024. * and the donor inode is also orig_page_offset
  1025. * (To easily handle blocksize != pagesize case, the offset for the
  1026. * donor inode is block unit).
  1027. * 5:Update holecheck_path and orig_path to points a next proceeding extent,
  1028. * then returns to step 2.
  1029. * 6:Release holecheck_path, orig_path and set the len to moved_len
  1030. * which shows the number of moved blocks.
  1031. * The moved_len is useful for the command to calculate the file offset
  1032. * for starting next move extent ioctl.
  1033. * 7:Return 0 on success, or a negative error value on failure.
  1034. */
  1035. int
  1036. ext4_move_extents(struct file *o_filp, struct file *d_filp,
  1037. __u64 orig_start, __u64 donor_start, __u64 len,
  1038. __u64 *moved_len)
  1039. {
  1040. struct inode *orig_inode = o_filp->f_dentry->d_inode;
  1041. struct inode *donor_inode = d_filp->f_dentry->d_inode;
  1042. struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
  1043. struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
  1044. ext4_lblk_t block_start = orig_start;
  1045. ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
  1046. ext4_lblk_t rest_blocks;
  1047. pgoff_t orig_page_offset = 0, seq_end_page;
  1048. int ret1, ret2, depth, last_extent = 0;
  1049. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  1050. int data_offset_in_page;
  1051. int block_len_in_page;
  1052. int uninit;
  1053. /* orig and donor should be different file */
  1054. if (orig_inode->i_ino == donor_inode->i_ino) {
  1055. ext4_debug("ext4 move extent: The argument files should not "
  1056. "be same file [ino:orig %lu, donor %lu]\n",
  1057. orig_inode->i_ino, donor_inode->i_ino);
  1058. return -EINVAL;
  1059. }
  1060. /* Protect orig and donor inodes against a truncate */
  1061. ret1 = mext_inode_double_lock(orig_inode, donor_inode);
  1062. if (ret1 < 0)
  1063. return ret1;
  1064. /* Protect extent tree against block allocations via delalloc */
  1065. double_down_write_data_sem(orig_inode, donor_inode);
  1066. /* Check the filesystem environment whether move_extent can be done */
  1067. ret1 = mext_check_arguments(orig_inode, donor_inode, orig_start,
  1068. donor_start, &len);
  1069. if (ret1)
  1070. goto out;
  1071. file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
  1072. block_end = block_start + len - 1;
  1073. if (file_end < block_end)
  1074. len -= block_end - file_end;
  1075. ret1 = get_ext_path(orig_inode, block_start, &orig_path);
  1076. if (ret1)
  1077. goto out;
  1078. /* Get path structure to check the hole */
  1079. ret1 = get_ext_path(orig_inode, block_start, &holecheck_path);
  1080. if (ret1)
  1081. goto out;
  1082. depth = ext_depth(orig_inode);
  1083. ext_cur = holecheck_path[depth].p_ext;
  1084. /*
  1085. * Get proper starting location of block replacement if block_start was
  1086. * within the hole.
  1087. */
  1088. if (le32_to_cpu(ext_cur->ee_block) +
  1089. ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
  1090. /*
  1091. * The hole exists between extents or the tail of
  1092. * original file.
  1093. */
  1094. last_extent = mext_next_extent(orig_inode,
  1095. holecheck_path, &ext_cur);
  1096. if (last_extent < 0) {
  1097. ret1 = last_extent;
  1098. goto out;
  1099. }
  1100. last_extent = mext_next_extent(orig_inode, orig_path,
  1101. &ext_dummy);
  1102. if (last_extent < 0) {
  1103. ret1 = last_extent;
  1104. goto out;
  1105. }
  1106. seq_start = le32_to_cpu(ext_cur->ee_block);
  1107. } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
  1108. /* The hole exists at the beginning of original file. */
  1109. seq_start = le32_to_cpu(ext_cur->ee_block);
  1110. else
  1111. seq_start = block_start;
  1112. /* No blocks within the specified range. */
  1113. if (le32_to_cpu(ext_cur->ee_block) > block_end) {
  1114. ext4_debug("ext4 move extent: The specified range of file "
  1115. "may be the hole\n");
  1116. ret1 = -EINVAL;
  1117. goto out;
  1118. }
  1119. /* Adjust start blocks */
  1120. add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
  1121. ext4_ext_get_actual_len(ext_cur), block_end + 1) -
  1122. max(le32_to_cpu(ext_cur->ee_block), block_start);
  1123. while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
  1124. seq_blocks += add_blocks;
  1125. /* Adjust tail blocks */
  1126. if (seq_start + seq_blocks - 1 > block_end)
  1127. seq_blocks = block_end - seq_start + 1;
  1128. ext_prev = ext_cur;
  1129. last_extent = mext_next_extent(orig_inode, holecheck_path,
  1130. &ext_cur);
  1131. if (last_extent < 0) {
  1132. ret1 = last_extent;
  1133. break;
  1134. }
  1135. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1136. /*
  1137. * Extend the length of contiguous block (seq_blocks)
  1138. * if extents are contiguous.
  1139. */
  1140. if (ext4_can_extents_be_merged(orig_inode,
  1141. ext_prev, ext_cur) &&
  1142. block_end >= le32_to_cpu(ext_cur->ee_block) &&
  1143. !last_extent)
  1144. continue;
  1145. /* Is original extent is uninitialized */
  1146. uninit = ext4_ext_is_uninitialized(ext_prev);
  1147. data_offset_in_page = seq_start % blocks_per_page;
  1148. /*
  1149. * Calculate data blocks count that should be swapped
  1150. * at the first page.
  1151. */
  1152. if (data_offset_in_page + seq_blocks > blocks_per_page) {
  1153. /* Swapped blocks are across pages */
  1154. block_len_in_page =
  1155. blocks_per_page - data_offset_in_page;
  1156. } else {
  1157. /* Swapped blocks are in a page */
  1158. block_len_in_page = seq_blocks;
  1159. }
  1160. orig_page_offset = seq_start >>
  1161. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1162. seq_end_page = (seq_start + seq_blocks - 1) >>
  1163. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1164. seq_start = le32_to_cpu(ext_cur->ee_block);
  1165. rest_blocks = seq_blocks;
  1166. /*
  1167. * Up semaphore to avoid following problems:
  1168. * a. transaction deadlock among ext4_journal_start,
  1169. * ->write_begin via pagefault, and jbd2_journal_commit
  1170. * b. racing with ->readpage, ->write_begin, and ext4_get_block
  1171. * in move_extent_per_page
  1172. */
  1173. double_up_write_data_sem(orig_inode, donor_inode);
  1174. while (orig_page_offset <= seq_end_page) {
  1175. /* Swap original branches with new branches */
  1176. block_len_in_page = move_extent_per_page(
  1177. o_filp, donor_inode,
  1178. orig_page_offset,
  1179. data_offset_in_page,
  1180. block_len_in_page, uninit,
  1181. &ret1);
  1182. /* Count how many blocks we have exchanged */
  1183. *moved_len += block_len_in_page;
  1184. if (ret1 < 0)
  1185. break;
  1186. if (*moved_len > len) {
  1187. ext4_error(orig_inode->i_sb, __func__,
  1188. "We replaced blocks too much! "
  1189. "sum of replaced: %llu requested: %llu",
  1190. *moved_len, len);
  1191. ret1 = -EIO;
  1192. break;
  1193. }
  1194. orig_page_offset++;
  1195. data_offset_in_page = 0;
  1196. rest_blocks -= block_len_in_page;
  1197. if (rest_blocks > blocks_per_page)
  1198. block_len_in_page = blocks_per_page;
  1199. else
  1200. block_len_in_page = rest_blocks;
  1201. }
  1202. double_down_write_data_sem(orig_inode, donor_inode);
  1203. if (ret1 < 0)
  1204. break;
  1205. /* Decrease buffer counter */
  1206. if (holecheck_path)
  1207. ext4_ext_drop_refs(holecheck_path);
  1208. ret1 = get_ext_path(orig_inode, seq_start, &holecheck_path);
  1209. if (ret1)
  1210. break;
  1211. depth = holecheck_path->p_depth;
  1212. /* Decrease buffer counter */
  1213. if (orig_path)
  1214. ext4_ext_drop_refs(orig_path);
  1215. ret1 = get_ext_path(orig_inode, seq_start, &orig_path);
  1216. if (ret1)
  1217. break;
  1218. ext_cur = holecheck_path[depth].p_ext;
  1219. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1220. seq_blocks = 0;
  1221. }
  1222. out:
  1223. if (*moved_len) {
  1224. ext4_discard_preallocations(orig_inode);
  1225. ext4_discard_preallocations(donor_inode);
  1226. }
  1227. if (orig_path) {
  1228. ext4_ext_drop_refs(orig_path);
  1229. kfree(orig_path);
  1230. }
  1231. if (holecheck_path) {
  1232. ext4_ext_drop_refs(holecheck_path);
  1233. kfree(holecheck_path);
  1234. }
  1235. double_up_write_data_sem(orig_inode, donor_inode);
  1236. ret2 = mext_inode_double_unlock(orig_inode, donor_inode);
  1237. if (ret1)
  1238. return ret1;
  1239. else if (ret2)
  1240. return ret2;
  1241. return 0;
  1242. }