move_extent.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
  847. ext4_debug("ext4 move extent: suid or sgid is set"
  848. " to donor file [ino:orig %lu, donor %lu]\n",
  849. orig_inode->i_ino, donor_inode->i_ino);
  850. return -EINVAL;
  851. }
  852. /* Ext4 move extent does not support swapfile */
  853. if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
  854. ext4_debug("ext4 move extent: The argument files should "
  855. "not be swapfile [ino:orig %lu, donor %lu]\n",
  856. orig_inode->i_ino, donor_inode->i_ino);
  857. return -EINVAL;
  858. }
  859. /* Files should be in the same ext4 FS */
  860. if (orig_inode->i_sb != donor_inode->i_sb) {
  861. ext4_debug("ext4 move extent: The argument files "
  862. "should be in same FS [ino:orig %lu, donor %lu]\n",
  863. orig_inode->i_ino, donor_inode->i_ino);
  864. return -EINVAL;
  865. }
  866. /* Ext4 move extent supports only extent based file */
  867. if (!(EXT4_I(orig_inode)->i_flags & EXT4_EXTENTS_FL)) {
  868. ext4_debug("ext4 move extent: orig file is not extents "
  869. "based file [ino:orig %lu]\n", orig_inode->i_ino);
  870. return -EOPNOTSUPP;
  871. } else if (!(EXT4_I(donor_inode)->i_flags & EXT4_EXTENTS_FL)) {
  872. ext4_debug("ext4 move extent: donor file is not extents "
  873. "based file [ino:donor %lu]\n", donor_inode->i_ino);
  874. return -EOPNOTSUPP;
  875. }
  876. if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
  877. ext4_debug("ext4 move extent: File size is 0 byte\n");
  878. return -EINVAL;
  879. }
  880. /* Start offset should be same */
  881. if (orig_start != donor_start) {
  882. ext4_debug("ext4 move extent: orig and donor's start "
  883. "offset are not same [ino:orig %lu, donor %lu]\n",
  884. orig_inode->i_ino, donor_inode->i_ino);
  885. return -EINVAL;
  886. }
  887. if ((orig_start > EXT_MAX_BLOCK) ||
  888. (donor_start > EXT_MAX_BLOCK) ||
  889. (*len > EXT_MAX_BLOCK) ||
  890. (orig_start + *len > EXT_MAX_BLOCK)) {
  891. ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
  892. "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCK,
  893. orig_inode->i_ino, donor_inode->i_ino);
  894. return -EINVAL;
  895. }
  896. if (orig_inode->i_size > donor_inode->i_size) {
  897. donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
  898. /* TODO: eliminate this artificial restriction */
  899. if (orig_start >= donor_blocks) {
  900. ext4_debug("ext4 move extent: orig start offset "
  901. "[%llu] should be less than donor file blocks "
  902. "[%u] [ino:orig %lu, donor %lu]\n",
  903. orig_start, donor_blocks,
  904. orig_inode->i_ino, donor_inode->i_ino);
  905. return -EINVAL;
  906. }
  907. /* TODO: eliminate this artificial restriction */
  908. if (orig_start + *len > donor_blocks) {
  909. ext4_debug("ext4 move extent: End offset [%llu] should "
  910. "be less than donor file blocks [%u]."
  911. "So adjust length from %llu to %llu "
  912. "[ino:orig %lu, donor %lu]\n",
  913. orig_start + *len, donor_blocks,
  914. *len, donor_blocks - orig_start,
  915. orig_inode->i_ino, donor_inode->i_ino);
  916. *len = donor_blocks - orig_start;
  917. }
  918. } else {
  919. orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
  920. if (orig_start >= orig_blocks) {
  921. ext4_debug("ext4 move extent: start offset [%llu] "
  922. "should be less than original file blocks "
  923. "[%u] [ino:orig %lu, donor %lu]\n",
  924. orig_start, orig_blocks,
  925. orig_inode->i_ino, donor_inode->i_ino);
  926. return -EINVAL;
  927. }
  928. if (orig_start + *len > orig_blocks) {
  929. ext4_debug("ext4 move extent: Adjust length "
  930. "from %llu to %llu. Because it should be "
  931. "less than original file blocks "
  932. "[ino:orig %lu, donor %lu]\n",
  933. *len, orig_blocks - orig_start,
  934. orig_inode->i_ino, donor_inode->i_ino);
  935. *len = orig_blocks - orig_start;
  936. }
  937. }
  938. if (!*len) {
  939. ext4_debug("ext4 move extent: len should not be 0 "
  940. "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
  941. donor_inode->i_ino);
  942. return -EINVAL;
  943. }
  944. return 0;
  945. }
  946. /**
  947. * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
  948. *
  949. * @inode1: the inode structure
  950. * @inode2: the inode structure
  951. *
  952. * Lock two inodes' i_mutex by i_ino order.
  953. * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
  954. */
  955. static int
  956. mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
  957. {
  958. int ret = 0;
  959. BUG_ON(inode1 == NULL && inode2 == NULL);
  960. ret = mext_check_null_inode(inode1, inode2, __func__);
  961. if (ret < 0)
  962. goto out;
  963. if (inode1 == inode2) {
  964. mutex_lock(&inode1->i_mutex);
  965. goto out;
  966. }
  967. if (inode1->i_ino < inode2->i_ino) {
  968. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  969. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  970. } else {
  971. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
  972. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
  973. }
  974. out:
  975. return ret;
  976. }
  977. /**
  978. * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
  979. *
  980. * @inode1: the inode that is released first
  981. * @inode2: the inode that is released second
  982. *
  983. * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
  984. */
  985. static int
  986. mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
  987. {
  988. int ret = 0;
  989. BUG_ON(inode1 == NULL && inode2 == NULL);
  990. ret = mext_check_null_inode(inode1, inode2, __func__);
  991. if (ret < 0)
  992. goto out;
  993. if (inode1)
  994. mutex_unlock(&inode1->i_mutex);
  995. if (inode2 && inode2 != inode1)
  996. mutex_unlock(&inode2->i_mutex);
  997. out:
  998. return ret;
  999. }
  1000. /**
  1001. * ext4_move_extents - Exchange the specified range of a file
  1002. *
  1003. * @o_filp: file structure of the original file
  1004. * @d_filp: file structure of the donor file
  1005. * @orig_start: start offset in block for orig
  1006. * @donor_start: start offset in block for donor
  1007. * @len: the number of blocks to be moved
  1008. * @moved_len: moved block length
  1009. *
  1010. * This function returns 0 and moved block length is set in moved_len
  1011. * if succeed, otherwise returns error value.
  1012. *
  1013. * Note: ext4_move_extents() proceeds the following order.
  1014. * 1:ext4_move_extents() calculates the last block number of moving extent
  1015. * function by the start block number (orig_start) and the number of blocks
  1016. * to be moved (len) specified as arguments.
  1017. * If the {orig, donor}_start points a hole, the extent's start offset
  1018. * pointed by ext_cur (current extent), holecheck_path, orig_path are set
  1019. * after hole behind.
  1020. * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
  1021. * or the ext_cur exceeds the block_end which is last logical block number.
  1022. * 3:To get the length of continues area, call mext_next_extent()
  1023. * specified with the ext_cur (initial value is holecheck_path) re-cursive,
  1024. * until find un-continuous extent, the start logical block number exceeds
  1025. * the block_end or the extent points to the last extent.
  1026. * 4:Exchange the original inode data with donor inode data
  1027. * from orig_page_offset to seq_end_page.
  1028. * The start indexes of data are specified as arguments.
  1029. * That of the original inode is orig_page_offset,
  1030. * and the donor inode is also orig_page_offset
  1031. * (To easily handle blocksize != pagesize case, the offset for the
  1032. * donor inode is block unit).
  1033. * 5:Update holecheck_path and orig_path to points a next proceeding extent,
  1034. * then returns to step 2.
  1035. * 6:Release holecheck_path, orig_path and set the len to moved_len
  1036. * which shows the number of moved blocks.
  1037. * The moved_len is useful for the command to calculate the file offset
  1038. * for starting next move extent ioctl.
  1039. * 7:Return 0 on success, or a negative error value on failure.
  1040. */
  1041. int
  1042. ext4_move_extents(struct file *o_filp, struct file *d_filp,
  1043. __u64 orig_start, __u64 donor_start, __u64 len,
  1044. __u64 *moved_len)
  1045. {
  1046. struct inode *orig_inode = o_filp->f_dentry->d_inode;
  1047. struct inode *donor_inode = d_filp->f_dentry->d_inode;
  1048. struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
  1049. struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
  1050. ext4_lblk_t block_start = orig_start;
  1051. ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
  1052. ext4_lblk_t rest_blocks;
  1053. pgoff_t orig_page_offset = 0, seq_end_page;
  1054. int ret1, ret2, depth, last_extent = 0;
  1055. int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
  1056. int data_offset_in_page;
  1057. int block_len_in_page;
  1058. int uninit;
  1059. /* orig and donor should be different file */
  1060. if (orig_inode->i_ino == donor_inode->i_ino) {
  1061. ext4_debug("ext4 move extent: The argument files should not "
  1062. "be same file [ino:orig %lu, donor %lu]\n",
  1063. orig_inode->i_ino, donor_inode->i_ino);
  1064. return -EINVAL;
  1065. }
  1066. /* Protect orig and donor inodes against a truncate */
  1067. ret1 = mext_inode_double_lock(orig_inode, donor_inode);
  1068. if (ret1 < 0)
  1069. return ret1;
  1070. /* Protect extent tree against block allocations via delalloc */
  1071. double_down_write_data_sem(orig_inode, donor_inode);
  1072. /* Check the filesystem environment whether move_extent can be done */
  1073. ret1 = mext_check_arguments(orig_inode, donor_inode, orig_start,
  1074. donor_start, &len);
  1075. if (ret1)
  1076. goto out;
  1077. file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
  1078. block_end = block_start + len - 1;
  1079. if (file_end < block_end)
  1080. len -= block_end - file_end;
  1081. ret1 = get_ext_path(orig_inode, block_start, &orig_path);
  1082. if (ret1)
  1083. goto out;
  1084. /* Get path structure to check the hole */
  1085. ret1 = get_ext_path(orig_inode, block_start, &holecheck_path);
  1086. if (ret1)
  1087. goto out;
  1088. depth = ext_depth(orig_inode);
  1089. ext_cur = holecheck_path[depth].p_ext;
  1090. /*
  1091. * Get proper starting location of block replacement if block_start was
  1092. * within the hole.
  1093. */
  1094. if (le32_to_cpu(ext_cur->ee_block) +
  1095. ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
  1096. /*
  1097. * The hole exists between extents or the tail of
  1098. * original file.
  1099. */
  1100. last_extent = mext_next_extent(orig_inode,
  1101. holecheck_path, &ext_cur);
  1102. if (last_extent < 0) {
  1103. ret1 = last_extent;
  1104. goto out;
  1105. }
  1106. last_extent = mext_next_extent(orig_inode, orig_path,
  1107. &ext_dummy);
  1108. if (last_extent < 0) {
  1109. ret1 = last_extent;
  1110. goto out;
  1111. }
  1112. seq_start = le32_to_cpu(ext_cur->ee_block);
  1113. } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
  1114. /* The hole exists at the beginning of original file. */
  1115. seq_start = le32_to_cpu(ext_cur->ee_block);
  1116. else
  1117. seq_start = block_start;
  1118. /* No blocks within the specified range. */
  1119. if (le32_to_cpu(ext_cur->ee_block) > block_end) {
  1120. ext4_debug("ext4 move extent: The specified range of file "
  1121. "may be the hole\n");
  1122. ret1 = -EINVAL;
  1123. goto out;
  1124. }
  1125. /* Adjust start blocks */
  1126. add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
  1127. ext4_ext_get_actual_len(ext_cur), block_end + 1) -
  1128. max(le32_to_cpu(ext_cur->ee_block), block_start);
  1129. while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
  1130. seq_blocks += add_blocks;
  1131. /* Adjust tail blocks */
  1132. if (seq_start + seq_blocks - 1 > block_end)
  1133. seq_blocks = block_end - seq_start + 1;
  1134. ext_prev = ext_cur;
  1135. last_extent = mext_next_extent(orig_inode, holecheck_path,
  1136. &ext_cur);
  1137. if (last_extent < 0) {
  1138. ret1 = last_extent;
  1139. break;
  1140. }
  1141. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1142. /*
  1143. * Extend the length of contiguous block (seq_blocks)
  1144. * if extents are contiguous.
  1145. */
  1146. if (ext4_can_extents_be_merged(orig_inode,
  1147. ext_prev, ext_cur) &&
  1148. block_end >= le32_to_cpu(ext_cur->ee_block) &&
  1149. !last_extent)
  1150. continue;
  1151. /* Is original extent is uninitialized */
  1152. uninit = ext4_ext_is_uninitialized(ext_prev);
  1153. data_offset_in_page = seq_start % blocks_per_page;
  1154. /*
  1155. * Calculate data blocks count that should be swapped
  1156. * at the first page.
  1157. */
  1158. if (data_offset_in_page + seq_blocks > blocks_per_page) {
  1159. /* Swapped blocks are across pages */
  1160. block_len_in_page =
  1161. blocks_per_page - data_offset_in_page;
  1162. } else {
  1163. /* Swapped blocks are in a page */
  1164. block_len_in_page = seq_blocks;
  1165. }
  1166. orig_page_offset = seq_start >>
  1167. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1168. seq_end_page = (seq_start + seq_blocks - 1) >>
  1169. (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
  1170. seq_start = le32_to_cpu(ext_cur->ee_block);
  1171. rest_blocks = seq_blocks;
  1172. /*
  1173. * Up semaphore to avoid following problems:
  1174. * a. transaction deadlock among ext4_journal_start,
  1175. * ->write_begin via pagefault, and jbd2_journal_commit
  1176. * b. racing with ->readpage, ->write_begin, and ext4_get_block
  1177. * in move_extent_per_page
  1178. */
  1179. double_up_write_data_sem(orig_inode, donor_inode);
  1180. while (orig_page_offset <= seq_end_page) {
  1181. /* Swap original branches with new branches */
  1182. block_len_in_page = move_extent_per_page(
  1183. o_filp, donor_inode,
  1184. orig_page_offset,
  1185. data_offset_in_page,
  1186. block_len_in_page, uninit,
  1187. &ret1);
  1188. /* Count how many blocks we have exchanged */
  1189. *moved_len += block_len_in_page;
  1190. if (ret1 < 0)
  1191. break;
  1192. if (*moved_len > len) {
  1193. ext4_error(orig_inode->i_sb, __func__,
  1194. "We replaced blocks too much! "
  1195. "sum of replaced: %llu requested: %llu",
  1196. *moved_len, len);
  1197. ret1 = -EIO;
  1198. break;
  1199. }
  1200. orig_page_offset++;
  1201. data_offset_in_page = 0;
  1202. rest_blocks -= block_len_in_page;
  1203. if (rest_blocks > blocks_per_page)
  1204. block_len_in_page = blocks_per_page;
  1205. else
  1206. block_len_in_page = rest_blocks;
  1207. }
  1208. double_down_write_data_sem(orig_inode, donor_inode);
  1209. if (ret1 < 0)
  1210. break;
  1211. /* Decrease buffer counter */
  1212. if (holecheck_path)
  1213. ext4_ext_drop_refs(holecheck_path);
  1214. ret1 = get_ext_path(orig_inode, seq_start, &holecheck_path);
  1215. if (ret1)
  1216. break;
  1217. depth = holecheck_path->p_depth;
  1218. /* Decrease buffer counter */
  1219. if (orig_path)
  1220. ext4_ext_drop_refs(orig_path);
  1221. ret1 = get_ext_path(orig_inode, seq_start, &orig_path);
  1222. if (ret1)
  1223. break;
  1224. ext_cur = holecheck_path[depth].p_ext;
  1225. add_blocks = ext4_ext_get_actual_len(ext_cur);
  1226. seq_blocks = 0;
  1227. }
  1228. out:
  1229. if (*moved_len) {
  1230. ext4_discard_preallocations(orig_inode);
  1231. ext4_discard_preallocations(donor_inode);
  1232. }
  1233. if (orig_path) {
  1234. ext4_ext_drop_refs(orig_path);
  1235. kfree(orig_path);
  1236. }
  1237. if (holecheck_path) {
  1238. ext4_ext_drop_refs(holecheck_path);
  1239. kfree(holecheck_path);
  1240. }
  1241. double_up_write_data_sem(orig_inode, donor_inode);
  1242. ret2 = mext_inode_double_unlock(orig_inode, donor_inode);
  1243. if (ret1)
  1244. return ret1;
  1245. else if (ret2)
  1246. return ret2;
  1247. return 0;
  1248. }