move_extent.c 44 KB

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