move_extent.c 41 KB

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