move_extent.c 38 KB

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