move_extents.c 25 KB

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