move_extents.c 25 KB

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