move_extents.c 26 KB

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