move_extents.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * move_extents.c
  4. *
  5. * Copyright (C) 2011 Oracle. All rights reserved.
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/types.h>
  9. #include <linux/mount.h>
  10. #include <linux/swap.h>
  11. #include <cluster/masklog.h>
  12. #include "ocfs2.h"
  13. #include "ocfs2_ioctl.h"
  14. #include "alloc.h"
  15. #include "localalloc.h"
  16. #include "aops.h"
  17. #include "dlmglue.h"
  18. #include "extent_map.h"
  19. #include "inode.h"
  20. #include "journal.h"
  21. #include "suballoc.h"
  22. #include "uptodate.h"
  23. #include "super.h"
  24. #include "dir.h"
  25. #include "buffer_head_io.h"
  26. #include "sysfile.h"
  27. #include "refcounttree.h"
  28. #include "move_extents.h"
  29. struct ocfs2_move_extents_context {
  30. struct inode *inode;
  31. struct file *file;
  32. int auto_defrag;
  33. int partial;
  34. int credits;
  35. u32 new_phys_cpos;
  36. u32 clusters_moved;
  37. u64 refcount_loc;
  38. struct ocfs2_move_extents *range;
  39. struct ocfs2_extent_tree et;
  40. struct ocfs2_alloc_context *meta_ac;
  41. struct ocfs2_alloc_context *data_ac;
  42. struct ocfs2_cached_dealloc_ctxt dealloc;
  43. };
  44. static int __ocfs2_move_extent(handle_t *handle,
  45. struct ocfs2_move_extents_context *context,
  46. u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos,
  47. int ext_flags)
  48. {
  49. int ret = 0, index;
  50. struct inode *inode = context->inode;
  51. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  52. struct ocfs2_extent_rec *rec, replace_rec;
  53. struct ocfs2_path *path = NULL;
  54. struct ocfs2_extent_list *el;
  55. u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci);
  56. u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos);
  57. ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos,
  58. p_cpos, new_p_cpos, len);
  59. if (ret) {
  60. mlog_errno(ret);
  61. goto out;
  62. }
  63. memset(&replace_rec, 0, sizeof(replace_rec));
  64. replace_rec.e_cpos = cpu_to_le32(cpos);
  65. replace_rec.e_leaf_clusters = cpu_to_le16(len);
  66. replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
  67. new_p_cpos));
  68. path = ocfs2_new_path_from_et(&context->et);
  69. if (!path) {
  70. ret = -ENOMEM;
  71. mlog_errno(ret);
  72. goto out;
  73. }
  74. ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos);
  75. if (ret) {
  76. mlog_errno(ret);
  77. goto out;
  78. }
  79. el = path_leaf_el(path);
  80. index = ocfs2_search_extent_list(el, cpos);
  81. if (index == -1) {
  82. ret = ocfs2_error(inode->i_sb,
  83. "Inode %llu has an extent at cpos %u which can no longer be found\n",
  84. (unsigned long long)ino, cpos);
  85. goto out;
  86. }
  87. rec = &el->l_recs[index];
  88. BUG_ON(ext_flags != rec->e_flags);
  89. /*
  90. * after moving/defraging to new location, the extent is not going
  91. * to be refcounted anymore.
  92. */
  93. replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
  94. ret = ocfs2_split_extent(handle, &context->et, path, index,
  95. &replace_rec, context->meta_ac,
  96. &context->dealloc);
  97. if (ret) {
  98. mlog_errno(ret);
  99. goto out;
  100. }
  101. context->new_phys_cpos = new_p_cpos;
  102. /*
  103. * need I to append truncate log for old clusters?
  104. */
  105. if (old_blkno) {
  106. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  107. ret = ocfs2_decrease_refcount(inode, handle,
  108. ocfs2_blocks_to_clusters(osb->sb,
  109. old_blkno),
  110. len, context->meta_ac,
  111. &context->dealloc, 1);
  112. else
  113. ret = ocfs2_truncate_log_append(osb, handle,
  114. old_blkno, len);
  115. }
  116. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  117. out:
  118. ocfs2_free_path(path);
  119. return ret;
  120. }
  121. /*
  122. * lock allocator, and reserve appropriate number of bits for
  123. * meta blocks.
  124. */
  125. static int ocfs2_lock_meta_allocator_move_extents(struct inode *inode,
  126. struct ocfs2_extent_tree *et,
  127. u32 clusters_to_move,
  128. u32 extents_to_split,
  129. struct ocfs2_alloc_context **meta_ac,
  130. int extra_blocks,
  131. int *credits)
  132. {
  133. int ret, num_free_extents;
  134. unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
  135. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  136. num_free_extents = ocfs2_num_free_extents(et);
  137. if (num_free_extents < 0) {
  138. ret = num_free_extents;
  139. mlog_errno(ret);
  140. goto out;
  141. }
  142. if (!num_free_extents ||
  143. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
  144. extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
  145. ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
  146. if (ret) {
  147. mlog_errno(ret);
  148. goto out;
  149. }
  150. *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el);
  151. mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
  152. extra_blocks, clusters_to_move, *credits);
  153. out:
  154. if (ret) {
  155. if (*meta_ac) {
  156. ocfs2_free_alloc_context(*meta_ac);
  157. *meta_ac = NULL;
  158. }
  159. }
  160. return ret;
  161. }
  162. /*
  163. * Using one journal handle to guarantee the data consistency in case
  164. * crash happens anywhere.
  165. *
  166. * XXX: defrag can end up with finishing partial extent as requested,
  167. * due to not enough contiguous clusters can be found in allocator.
  168. */
  169. static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
  170. u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
  171. {
  172. int ret, credits = 0, extra_blocks = 0, partial = context->partial;
  173. handle_t *handle;
  174. struct inode *inode = context->inode;
  175. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  176. struct inode *tl_inode = osb->osb_tl_inode;
  177. struct ocfs2_refcount_tree *ref_tree = NULL;
  178. u32 new_phys_cpos, new_len;
  179. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  180. int need_free = 0;
  181. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
  182. BUG_ON(!ocfs2_is_refcount_inode(inode));
  183. BUG_ON(!context->refcount_loc);
  184. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  185. &ref_tree, NULL);
  186. if (ret) {
  187. mlog_errno(ret);
  188. return ret;
  189. }
  190. ret = ocfs2_prepare_refcount_change_for_del(inode,
  191. context->refcount_loc,
  192. phys_blkno,
  193. *len,
  194. &credits,
  195. &extra_blocks);
  196. if (ret) {
  197. mlog_errno(ret);
  198. goto out;
  199. }
  200. }
  201. ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
  202. *len, 1,
  203. &context->meta_ac,
  204. extra_blocks, &credits);
  205. if (ret) {
  206. mlog_errno(ret);
  207. goto out;
  208. }
  209. /*
  210. * should be using allocation reservation strategy there?
  211. *
  212. * if (context->data_ac)
  213. * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  214. */
  215. inode_lock(tl_inode);
  216. if (ocfs2_truncate_log_needs_flush(osb)) {
  217. ret = __ocfs2_flush_truncate_log(osb);
  218. if (ret < 0) {
  219. mlog_errno(ret);
  220. goto out_unlock_mutex;
  221. }
  222. }
  223. /*
  224. * Make sure ocfs2_reserve_cluster is called after
  225. * __ocfs2_flush_truncate_log, otherwise, dead lock may happen.
  226. *
  227. * If ocfs2_reserve_cluster is called
  228. * before __ocfs2_flush_truncate_log, dead lock on global bitmap
  229. * may happen.
  230. *
  231. */
  232. ret = ocfs2_reserve_clusters(osb, *len, &context->data_ac);
  233. if (ret) {
  234. mlog_errno(ret);
  235. goto out_unlock_mutex;
  236. }
  237. handle = ocfs2_start_trans(osb, credits);
  238. if (IS_ERR(handle)) {
  239. ret = PTR_ERR(handle);
  240. mlog_errno(ret);
  241. goto out_unlock_mutex;
  242. }
  243. ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
  244. &new_phys_cpos, &new_len);
  245. if (ret) {
  246. mlog_errno(ret);
  247. goto out_commit;
  248. }
  249. /*
  250. * allowing partial extent moving is kind of 'pros and cons', it makes
  251. * whole defragmentation less likely to fail, on the contrary, the bad
  252. * thing is it may make the fs even more fragmented after moving, let
  253. * userspace make a good decision here.
  254. */
  255. if (new_len != *len) {
  256. mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
  257. if (!partial) {
  258. context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
  259. ret = -ENOSPC;
  260. need_free = 1;
  261. goto out_commit;
  262. }
  263. }
  264. mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
  265. phys_cpos, new_phys_cpos);
  266. ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
  267. new_phys_cpos, ext_flags);
  268. if (ret)
  269. mlog_errno(ret);
  270. if (partial && (new_len != *len))
  271. *len = new_len;
  272. /*
  273. * Here we should write the new page out first if we are
  274. * in write-back mode.
  275. */
  276. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
  277. if (ret)
  278. mlog_errno(ret);
  279. out_commit:
  280. if (need_free && context->data_ac) {
  281. struct ocfs2_alloc_context *data_ac = context->data_ac;
  282. if (context->data_ac->ac_which == OCFS2_AC_USE_LOCAL)
  283. ocfs2_free_local_alloc_bits(osb, handle, data_ac,
  284. new_phys_cpos, new_len);
  285. else
  286. ocfs2_free_clusters(handle,
  287. data_ac->ac_inode,
  288. data_ac->ac_bh,
  289. ocfs2_clusters_to_blocks(osb->sb, new_phys_cpos),
  290. new_len);
  291. }
  292. ocfs2_commit_trans(osb, handle);
  293. out_unlock_mutex:
  294. inode_unlock(tl_inode);
  295. if (context->data_ac) {
  296. ocfs2_free_alloc_context(context->data_ac);
  297. context->data_ac = NULL;
  298. }
  299. if (context->meta_ac) {
  300. ocfs2_free_alloc_context(context->meta_ac);
  301. context->meta_ac = NULL;
  302. }
  303. out:
  304. if (ref_tree)
  305. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  306. return ret;
  307. }
  308. /*
  309. * find the victim alloc group, where #blkno fits.
  310. */
  311. static int ocfs2_find_victim_alloc_group(struct inode *inode,
  312. u64 vict_blkno,
  313. int type, int slot,
  314. int *vict_bit,
  315. struct buffer_head **ret_bh)
  316. {
  317. int ret, i, bits_per_unit = 0;
  318. u64 blkno;
  319. char namebuf[40];
  320. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  321. struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
  322. struct ocfs2_chain_list *cl;
  323. struct ocfs2_chain_rec *rec;
  324. struct ocfs2_dinode *ac_dinode;
  325. struct ocfs2_group_desc *bg;
  326. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
  327. ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
  328. strlen(namebuf), &blkno);
  329. if (ret) {
  330. ret = -ENOENT;
  331. goto out;
  332. }
  333. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
  334. if (ret) {
  335. mlog_errno(ret);
  336. goto out;
  337. }
  338. ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
  339. cl = &(ac_dinode->id2.i_chain);
  340. rec = &(cl->cl_recs[0]);
  341. if (type == GLOBAL_BITMAP_SYSTEM_INODE)
  342. bits_per_unit = osb->s_clustersize_bits -
  343. inode->i_sb->s_blocksize_bits;
  344. /*
  345. * 'vict_blkno' was out of the valid range.
  346. */
  347. if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
  348. (vict_blkno >= ((u64)le32_to_cpu(ac_dinode->id1.bitmap1.i_total) <<
  349. bits_per_unit))) {
  350. ret = -EINVAL;
  351. goto out;
  352. }
  353. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  354. rec = &(cl->cl_recs[i]);
  355. if (!rec)
  356. continue;
  357. bg = NULL;
  358. do {
  359. if (!bg)
  360. blkno = le64_to_cpu(rec->c_blkno);
  361. else
  362. blkno = le64_to_cpu(bg->bg_next_group);
  363. if (gd_bh) {
  364. brelse(gd_bh);
  365. gd_bh = NULL;
  366. }
  367. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
  368. if (ret) {
  369. mlog_errno(ret);
  370. goto out;
  371. }
  372. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  373. if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
  374. (le16_to_cpu(bg->bg_bits) << bits_per_unit))) {
  375. *ret_bh = gd_bh;
  376. *vict_bit = (vict_blkno - blkno) >>
  377. bits_per_unit;
  378. mlog(0, "find the victim group: #%llu, "
  379. "total_bits: %u, vict_bit: %u\n",
  380. blkno, le16_to_cpu(bg->bg_bits),
  381. *vict_bit);
  382. goto out;
  383. }
  384. } while (le64_to_cpu(bg->bg_next_group));
  385. }
  386. ret = -EINVAL;
  387. out:
  388. brelse(ac_bh);
  389. /*
  390. * caller has to release the gd_bh properly.
  391. */
  392. return ret;
  393. }
  394. /*
  395. * XXX: helper to validate and adjust moving goal.
  396. */
  397. static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
  398. struct ocfs2_move_extents *range)
  399. {
  400. int ret, goal_bit = 0;
  401. struct buffer_head *gd_bh = NULL;
  402. struct ocfs2_group_desc *bg;
  403. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  404. int c_to_b = 1 << (osb->s_clustersize_bits -
  405. inode->i_sb->s_blocksize_bits);
  406. /*
  407. * make goal become cluster aligned.
  408. */
  409. range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb,
  410. range->me_goal);
  411. /*
  412. * validate goal sits within global_bitmap, and return the victim
  413. * group desc
  414. */
  415. ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
  416. GLOBAL_BITMAP_SYSTEM_INODE,
  417. OCFS2_INVALID_SLOT,
  418. &goal_bit, &gd_bh);
  419. if (ret)
  420. goto out;
  421. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  422. /*
  423. * moving goal is not allowd to start with a group desc blok(#0 blk)
  424. * let's compromise to the latter cluster.
  425. */
  426. if (range->me_goal == le64_to_cpu(bg->bg_blkno))
  427. range->me_goal += c_to_b;
  428. /*
  429. * movement is not gonna cross two groups.
  430. */
  431. if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
  432. range->me_len) {
  433. ret = -EINVAL;
  434. goto out;
  435. }
  436. /*
  437. * more exact validations/adjustments will be performed later during
  438. * moving operation for each extent range.
  439. */
  440. mlog(0, "extents get ready to be moved to #%llu block\n",
  441. range->me_goal);
  442. out:
  443. brelse(gd_bh);
  444. return ret;
  445. }
  446. static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
  447. int *goal_bit, u32 move_len, u32 max_hop,
  448. u32 *phys_cpos)
  449. {
  450. int i, used, last_free_bits = 0, base_bit = *goal_bit;
  451. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  452. u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  453. le64_to_cpu(gd->bg_blkno));
  454. for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
  455. used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
  456. if (used) {
  457. /*
  458. * we even tried searching the free chunk by jumping
  459. * a 'max_hop' distance, but still failed.
  460. */
  461. if ((i - base_bit) > max_hop) {
  462. *phys_cpos = 0;
  463. break;
  464. }
  465. if (last_free_bits)
  466. last_free_bits = 0;
  467. continue;
  468. } else
  469. last_free_bits++;
  470. if (last_free_bits == move_len) {
  471. i -= move_len;
  472. *goal_bit = i;
  473. *phys_cpos = base_cpos + i;
  474. break;
  475. }
  476. }
  477. mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
  478. }
  479. static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
  480. u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
  481. u32 len, int ext_flags)
  482. {
  483. int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
  484. handle_t *handle;
  485. struct inode *inode = context->inode;
  486. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  487. struct inode *tl_inode = osb->osb_tl_inode;
  488. struct inode *gb_inode = NULL;
  489. struct buffer_head *gb_bh = NULL;
  490. struct buffer_head *gd_bh = NULL;
  491. struct ocfs2_group_desc *gd;
  492. struct ocfs2_refcount_tree *ref_tree = NULL;
  493. u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
  494. context->range->me_threshold);
  495. u64 phys_blkno, new_phys_blkno;
  496. phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  497. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
  498. BUG_ON(!ocfs2_is_refcount_inode(inode));
  499. BUG_ON(!context->refcount_loc);
  500. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  501. &ref_tree, NULL);
  502. if (ret) {
  503. mlog_errno(ret);
  504. return ret;
  505. }
  506. ret = ocfs2_prepare_refcount_change_for_del(inode,
  507. context->refcount_loc,
  508. phys_blkno,
  509. len,
  510. &credits,
  511. &extra_blocks);
  512. if (ret) {
  513. mlog_errno(ret);
  514. goto out;
  515. }
  516. }
  517. ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et,
  518. len, 1,
  519. &context->meta_ac,
  520. extra_blocks, &credits);
  521. if (ret) {
  522. mlog_errno(ret);
  523. goto out;
  524. }
  525. /*
  526. * need to count 2 extra credits for global_bitmap inode and
  527. * group descriptor.
  528. */
  529. credits += OCFS2_INODE_UPDATE_CREDITS + 1;
  530. /*
  531. * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
  532. * logic, while we still need to lock the global_bitmap.
  533. */
  534. gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  535. OCFS2_INVALID_SLOT);
  536. if (!gb_inode) {
  537. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  538. ret = -EIO;
  539. goto out;
  540. }
  541. inode_lock(gb_inode);
  542. ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
  543. if (ret) {
  544. mlog_errno(ret);
  545. goto out_unlock_gb_mutex;
  546. }
  547. inode_lock(tl_inode);
  548. handle = ocfs2_start_trans(osb, credits);
  549. if (IS_ERR(handle)) {
  550. ret = PTR_ERR(handle);
  551. mlog_errno(ret);
  552. goto out_unlock_tl_inode;
  553. }
  554. new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
  555. ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
  556. GLOBAL_BITMAP_SYSTEM_INODE,
  557. OCFS2_INVALID_SLOT,
  558. &goal_bit, &gd_bh);
  559. if (ret) {
  560. mlog_errno(ret);
  561. goto out_commit;
  562. }
  563. /*
  564. * probe the victim cluster group to find a proper
  565. * region to fit wanted movement, it even will perfrom
  566. * a best-effort attempt by compromising to a threshold
  567. * around the goal.
  568. */
  569. ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
  570. new_phys_cpos);
  571. if (!*new_phys_cpos) {
  572. ret = -ENOSPC;
  573. goto out_commit;
  574. }
  575. ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
  576. *new_phys_cpos, ext_flags);
  577. if (ret) {
  578. mlog_errno(ret);
  579. goto out_commit;
  580. }
  581. gd = (struct ocfs2_group_desc *)gd_bh->b_data;
  582. ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
  583. le16_to_cpu(gd->bg_chain));
  584. if (ret) {
  585. mlog_errno(ret);
  586. goto out_commit;
  587. }
  588. ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
  589. goal_bit, len);
  590. if (ret) {
  591. ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
  592. le16_to_cpu(gd->bg_chain));
  593. mlog_errno(ret);
  594. }
  595. /*
  596. * Here we should write the new page out first if we are
  597. * in write-back mode.
  598. */
  599. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
  600. if (ret)
  601. mlog_errno(ret);
  602. out_commit:
  603. ocfs2_commit_trans(osb, handle);
  604. brelse(gd_bh);
  605. out_unlock_tl_inode:
  606. inode_unlock(tl_inode);
  607. ocfs2_inode_unlock(gb_inode, 1);
  608. out_unlock_gb_mutex:
  609. inode_unlock(gb_inode);
  610. brelse(gb_bh);
  611. iput(gb_inode);
  612. out:
  613. if (context->meta_ac) {
  614. ocfs2_free_alloc_context(context->meta_ac);
  615. context->meta_ac = NULL;
  616. }
  617. if (ref_tree)
  618. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  619. return ret;
  620. }
  621. /*
  622. * Helper to calculate the defraging length in one run according to threshold.
  623. */
  624. static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged,
  625. u32 threshold, int *skip)
  626. {
  627. if ((*alloc_size + *len_defraged) < threshold) {
  628. /*
  629. * proceed defragmentation until we meet the thresh
  630. */
  631. *len_defraged += *alloc_size;
  632. } else if (*len_defraged == 0) {
  633. /*
  634. * XXX: skip a large extent.
  635. */
  636. *skip = 1;
  637. } else {
  638. /*
  639. * split this extent to coalesce with former pieces as
  640. * to reach the threshold.
  641. *
  642. * we're done here with one cycle of defragmentation
  643. * in a size of 'thresh', resetting 'len_defraged'
  644. * forces a new defragmentation.
  645. */
  646. *alloc_size = threshold - *len_defraged;
  647. *len_defraged = 0;
  648. }
  649. }
  650. static int __ocfs2_move_extents_range(struct buffer_head *di_bh,
  651. struct ocfs2_move_extents_context *context)
  652. {
  653. int ret = 0, flags, do_defrag, skip = 0;
  654. u32 cpos, phys_cpos, move_start, len_to_move, alloc_size;
  655. u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0;
  656. struct inode *inode = context->inode;
  657. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  658. struct ocfs2_move_extents *range = context->range;
  659. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  660. if ((i_size_read(inode) == 0) || (range->me_len == 0))
  661. return 0;
  662. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  663. return 0;
  664. context->refcount_loc = le64_to_cpu(di->i_refcount_loc);
  665. ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh);
  666. ocfs2_init_dealloc_ctxt(&context->dealloc);
  667. /*
  668. * TO-DO XXX:
  669. *
  670. * - xattr extents.
  671. */
  672. do_defrag = context->auto_defrag;
  673. /*
  674. * extents moving happens in unit of clusters, for the sake
  675. * of simplicity, we may ignore two clusters where 'byte_start'
  676. * and 'byte_start + len' were within.
  677. */
  678. move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start);
  679. len_to_move = (range->me_start + range->me_len) >>
  680. osb->s_clustersize_bits;
  681. if (len_to_move >= move_start)
  682. len_to_move -= move_start;
  683. else
  684. len_to_move = 0;
  685. if (do_defrag) {
  686. defrag_thresh = range->me_threshold >> osb->s_clustersize_bits;
  687. if (defrag_thresh <= 1)
  688. goto done;
  689. } else
  690. new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  691. range->me_goal);
  692. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, "
  693. "thresh: %u\n",
  694. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  695. (unsigned long long)range->me_start,
  696. (unsigned long long)range->me_len,
  697. move_start, len_to_move, defrag_thresh);
  698. cpos = move_start;
  699. while (len_to_move) {
  700. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size,
  701. &flags);
  702. if (ret) {
  703. mlog_errno(ret);
  704. goto out;
  705. }
  706. if (alloc_size > len_to_move)
  707. alloc_size = len_to_move;
  708. /*
  709. * XXX: how to deal with a hole:
  710. *
  711. * - skip the hole of course
  712. * - force a new defragmentation
  713. */
  714. if (!phys_cpos) {
  715. if (do_defrag)
  716. len_defraged = 0;
  717. goto next;
  718. }
  719. if (do_defrag) {
  720. ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged,
  721. defrag_thresh, &skip);
  722. /*
  723. * skip large extents
  724. */
  725. if (skip) {
  726. skip = 0;
  727. goto next;
  728. }
  729. mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, "
  730. "alloc_size: %u, len_defraged: %u\n",
  731. cpos, phys_cpos, alloc_size, len_defraged);
  732. ret = ocfs2_defrag_extent(context, cpos, phys_cpos,
  733. &alloc_size, flags);
  734. } else {
  735. ret = ocfs2_move_extent(context, cpos, phys_cpos,
  736. &new_phys_cpos, alloc_size,
  737. flags);
  738. new_phys_cpos += alloc_size;
  739. }
  740. if (ret < 0) {
  741. mlog_errno(ret);
  742. goto out;
  743. }
  744. context->clusters_moved += alloc_size;
  745. next:
  746. cpos += alloc_size;
  747. len_to_move -= alloc_size;
  748. }
  749. done:
  750. range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE;
  751. out:
  752. range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb,
  753. context->clusters_moved);
  754. range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb,
  755. context->new_phys_cpos);
  756. ocfs2_schedule_truncate_log_flush(osb, 1);
  757. ocfs2_run_deallocs(osb, &context->dealloc);
  758. return ret;
  759. }
  760. static int ocfs2_move_extents(struct ocfs2_move_extents_context *context)
  761. {
  762. int status;
  763. handle_t *handle;
  764. struct inode *inode = context->inode;
  765. struct ocfs2_dinode *di;
  766. struct buffer_head *di_bh = NULL;
  767. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  768. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  769. return -EROFS;
  770. inode_lock(inode);
  771. /*
  772. * This prevents concurrent writes from other nodes
  773. */
  774. status = ocfs2_rw_lock(inode, 1);
  775. if (status) {
  776. mlog_errno(status);
  777. goto out;
  778. }
  779. status = ocfs2_inode_lock(inode, &di_bh, 1);
  780. if (status) {
  781. mlog_errno(status);
  782. goto out_rw_unlock;
  783. }
  784. /*
  785. * rememer ip_xattr_sem also needs to be held if necessary
  786. */
  787. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  788. status = __ocfs2_move_extents_range(di_bh, context);
  789. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  790. if (status) {
  791. mlog_errno(status);
  792. goto out_inode_unlock;
  793. }
  794. /*
  795. * We update ctime for these changes
  796. */
  797. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  798. if (IS_ERR(handle)) {
  799. status = PTR_ERR(handle);
  800. mlog_errno(status);
  801. goto out_inode_unlock;
  802. }
  803. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  804. OCFS2_JOURNAL_ACCESS_WRITE);
  805. if (status) {
  806. mlog_errno(status);
  807. goto out_commit;
  808. }
  809. di = (struct ocfs2_dinode *)di_bh->b_data;
  810. inode->i_ctime = current_time(inode);
  811. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  812. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  813. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  814. ocfs2_journal_dirty(handle, di_bh);
  815. out_commit:
  816. ocfs2_commit_trans(osb, handle);
  817. out_inode_unlock:
  818. brelse(di_bh);
  819. ocfs2_inode_unlock(inode, 1);
  820. out_rw_unlock:
  821. ocfs2_rw_unlock(inode, 1);
  822. out:
  823. inode_unlock(inode);
  824. return status;
  825. }
  826. int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
  827. {
  828. int status;
  829. struct inode *inode = file_inode(filp);
  830. struct ocfs2_move_extents range;
  831. struct ocfs2_move_extents_context *context;
  832. if (!argp)
  833. return -EINVAL;
  834. status = mnt_want_write_file(filp);
  835. if (status)
  836. return status;
  837. if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
  838. status = -EPERM;
  839. goto out_drop;
  840. }
  841. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  842. status = -EPERM;
  843. goto out_drop;
  844. }
  845. context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
  846. if (!context) {
  847. status = -ENOMEM;
  848. mlog_errno(status);
  849. goto out_drop;
  850. }
  851. context->inode = inode;
  852. context->file = filp;
  853. if (copy_from_user(&range, argp, sizeof(range))) {
  854. status = -EFAULT;
  855. goto out_free;
  856. }
  857. if (range.me_start > i_size_read(inode)) {
  858. status = -EINVAL;
  859. goto out_free;
  860. }
  861. if (range.me_start + range.me_len > i_size_read(inode))
  862. range.me_len = i_size_read(inode) - range.me_start;
  863. context->range = &range;
  864. /*
  865. * ok, the default theshold for the defragmentation
  866. * is 1M, since our maximum clustersize was 1M also.
  867. * any thought?
  868. */
  869. if (!range.me_threshold)
  870. range.me_threshold = 1024 * 1024;
  871. if (range.me_threshold > i_size_read(inode))
  872. range.me_threshold = i_size_read(inode);
  873. if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
  874. context->auto_defrag = 1;
  875. if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
  876. context->partial = 1;
  877. } else {
  878. /*
  879. * first best-effort attempt to validate and adjust the goal
  880. * (physical address in block), while it can't guarantee later
  881. * operation can succeed all the time since global_bitmap may
  882. * change a bit over time.
  883. */
  884. status = ocfs2_validate_and_adjust_move_goal(inode, &range);
  885. if (status)
  886. goto out_copy;
  887. }
  888. status = ocfs2_move_extents(context);
  889. if (status)
  890. mlog_errno(status);
  891. out_copy:
  892. /*
  893. * movement/defragmentation may end up being partially completed,
  894. * that's the reason why we need to return userspace the finished
  895. * length and new_offset even if failure happens somewhere.
  896. */
  897. if (copy_to_user(argp, &range, sizeof(range)))
  898. status = -EFAULT;
  899. out_free:
  900. kfree(context);
  901. out_drop:
  902. mnt_drop_write_file(filp);
  903. return status;
  904. }