move_extents.c 25 KB

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