move_extents.c 25 KB

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