move_extent.c 41 KB

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