move_extents.c 27 KB

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