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