move_extents.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * move_extents.c
  5. *
  6. * Copyright (C) 2011 Oracle. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/fs.h>
  18. #include <linux/types.h>
  19. #include <linux/mount.h>
  20. #include <linux/swap.h>
  21. #include <cluster/masklog.h>
  22. #include "ocfs2.h"
  23. #include "ocfs2_ioctl.h"
  24. #include "alloc.h"
  25. #include "aops.h"
  26. #include "dlmglue.h"
  27. #include "extent_map.h"
  28. #include "inode.h"
  29. #include "journal.h"
  30. #include "suballoc.h"
  31. #include "uptodate.h"
  32. #include "super.h"
  33. #include "dir.h"
  34. #include "buffer_head_io.h"
  35. #include "sysfile.h"
  36. #include "refcounttree.h"
  37. #include "move_extents.h"
  38. struct ocfs2_move_extents_context {
  39. struct inode *inode;
  40. struct file *file;
  41. int auto_defrag;
  42. int partial;
  43. int credits;
  44. u32 new_phys_cpos;
  45. u32 clusters_moved;
  46. u64 refcount_loc;
  47. struct ocfs2_move_extents *range;
  48. struct ocfs2_extent_tree et;
  49. struct ocfs2_alloc_context *meta_ac;
  50. struct ocfs2_alloc_context *data_ac;
  51. struct ocfs2_cached_dealloc_ctxt dealloc;
  52. };
  53. static int __ocfs2_move_extent(handle_t *handle,
  54. struct ocfs2_move_extents_context *context,
  55. u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos,
  56. int ext_flags)
  57. {
  58. int ret = 0, index;
  59. struct inode *inode = context->inode;
  60. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  61. struct ocfs2_extent_rec *rec, replace_rec;
  62. struct ocfs2_path *path = NULL;
  63. struct ocfs2_extent_list *el;
  64. u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci);
  65. u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos);
  66. ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos,
  67. p_cpos, new_p_cpos, len);
  68. if (ret) {
  69. mlog_errno(ret);
  70. goto out;
  71. }
  72. memset(&replace_rec, 0, sizeof(replace_rec));
  73. replace_rec.e_cpos = cpu_to_le32(cpos);
  74. replace_rec.e_leaf_clusters = cpu_to_le16(len);
  75. replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
  76. new_p_cpos));
  77. path = ocfs2_new_path_from_et(&context->et);
  78. if (!path) {
  79. ret = -ENOMEM;
  80. mlog_errno(ret);
  81. goto out;
  82. }
  83. ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos);
  84. if (ret) {
  85. mlog_errno(ret);
  86. goto out;
  87. }
  88. el = path_leaf_el(path);
  89. index = ocfs2_search_extent_list(el, cpos);
  90. if (index == -1) {
  91. ret = ocfs2_error(inode->i_sb,
  92. "Inode %llu has an extent at cpos %u which can no longer be found\n",
  93. (unsigned long long)ino, cpos);
  94. goto out;
  95. }
  96. rec = &el->l_recs[index];
  97. BUG_ON(ext_flags != rec->e_flags);
  98. /*
  99. * after moving/defraging to new location, the extent is not going
  100. * to be refcounted anymore.
  101. */
  102. replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
  103. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  104. context->et.et_root_bh,
  105. OCFS2_JOURNAL_ACCESS_WRITE);
  106. if (ret) {
  107. mlog_errno(ret);
  108. goto out;
  109. }
  110. ret = ocfs2_split_extent(handle, &context->et, path, index,
  111. &replace_rec, context->meta_ac,
  112. &context->dealloc);
  113. if (ret) {
  114. mlog_errno(ret);
  115. goto out;
  116. }
  117. ocfs2_journal_dirty(handle, context->et.et_root_bh);
  118. context->new_phys_cpos = new_p_cpos;
  119. /*
  120. * need I to append truncate log for old clusters?
  121. */
  122. if (old_blkno) {
  123. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  124. ret = ocfs2_decrease_refcount(inode, handle,
  125. ocfs2_blocks_to_clusters(osb->sb,
  126. old_blkno),
  127. len, context->meta_ac,
  128. &context->dealloc, 1);
  129. else
  130. ret = ocfs2_truncate_log_append(osb, handle,
  131. old_blkno, len);
  132. }
  133. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  134. out:
  135. ocfs2_free_path(path);
  136. return ret;
  137. }
  138. /*
  139. * lock 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. mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
  180. extra_blocks, clusters_to_move, *credits);
  181. out:
  182. if (ret) {
  183. if (*meta_ac) {
  184. ocfs2_free_alloc_context(*meta_ac);
  185. *meta_ac = NULL;
  186. }
  187. }
  188. return ret;
  189. }
  190. /*
  191. * Using one journal handle to guarantee the data consistency in case
  192. * crash happens anywhere.
  193. *
  194. * XXX: defrag can end up with finishing partial extent as requested,
  195. * due to not enough contiguous clusters can be found in allocator.
  196. */
  197. static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
  198. u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
  199. {
  200. int ret, credits = 0, extra_blocks = 0, partial = context->partial;
  201. handle_t *handle;
  202. struct inode *inode = context->inode;
  203. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  204. struct inode *tl_inode = osb->osb_tl_inode;
  205. struct ocfs2_refcount_tree *ref_tree = NULL;
  206. u32 new_phys_cpos, new_len;
  207. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  208. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
  209. BUG_ON(!ocfs2_is_refcount_inode(inode));
  210. BUG_ON(!context->refcount_loc);
  211. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  212. &ref_tree, NULL);
  213. if (ret) {
  214. mlog_errno(ret);
  215. return ret;
  216. }
  217. ret = ocfs2_prepare_refcount_change_for_del(inode,
  218. context->refcount_loc,
  219. phys_blkno,
  220. *len,
  221. &credits,
  222. &extra_blocks);
  223. if (ret) {
  224. mlog_errno(ret);
  225. goto out;
  226. }
  227. }
  228. ret = ocfs2_lock_allocators_move_extents(inode, &context->et, *len, 1,
  229. &context->meta_ac,
  230. &context->data_ac,
  231. extra_blocks, &credits);
  232. if (ret) {
  233. mlog_errno(ret);
  234. goto out;
  235. }
  236. /*
  237. * should be using allocation reservation strategy there?
  238. *
  239. * if (context->data_ac)
  240. * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  241. */
  242. inode_lock(tl_inode);
  243. if (ocfs2_truncate_log_needs_flush(osb)) {
  244. ret = __ocfs2_flush_truncate_log(osb);
  245. if (ret < 0) {
  246. mlog_errno(ret);
  247. goto out_unlock_mutex;
  248. }
  249. }
  250. handle = ocfs2_start_trans(osb, credits);
  251. if (IS_ERR(handle)) {
  252. ret = PTR_ERR(handle);
  253. mlog_errno(ret);
  254. goto out_unlock_mutex;
  255. }
  256. ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
  257. &new_phys_cpos, &new_len);
  258. if (ret) {
  259. mlog_errno(ret);
  260. goto out_commit;
  261. }
  262. /*
  263. * allowing partial extent moving is kind of 'pros and cons', it makes
  264. * whole defragmentation less likely to fail, on the contrary, the bad
  265. * thing is it may make the fs even more fragmented after moving, let
  266. * userspace make a good decision here.
  267. */
  268. if (new_len != *len) {
  269. mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
  270. if (!partial) {
  271. context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
  272. ret = -ENOSPC;
  273. goto out_commit;
  274. }
  275. }
  276. mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
  277. phys_cpos, new_phys_cpos);
  278. ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
  279. new_phys_cpos, ext_flags);
  280. if (ret)
  281. mlog_errno(ret);
  282. if (partial && (new_len != *len))
  283. *len = new_len;
  284. /*
  285. * Here we should write the new page out first if we are
  286. * in write-back mode.
  287. */
  288. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
  289. if (ret)
  290. mlog_errno(ret);
  291. out_commit:
  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))) {
  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. *goal_bit = i;
  472. *phys_cpos = base_cpos + i;
  473. break;
  474. }
  475. }
  476. mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
  477. }
  478. static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
  479. u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
  480. u32 len, int ext_flags)
  481. {
  482. int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
  483. handle_t *handle;
  484. struct inode *inode = context->inode;
  485. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  486. struct inode *tl_inode = osb->osb_tl_inode;
  487. struct inode *gb_inode = NULL;
  488. struct buffer_head *gb_bh = NULL;
  489. struct buffer_head *gd_bh = NULL;
  490. struct ocfs2_group_desc *gd;
  491. struct ocfs2_refcount_tree *ref_tree = NULL;
  492. u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
  493. context->range->me_threshold);
  494. u64 phys_blkno, new_phys_blkno;
  495. phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  496. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
  497. BUG_ON(!ocfs2_is_refcount_inode(inode));
  498. BUG_ON(!context->refcount_loc);
  499. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  500. &ref_tree, NULL);
  501. if (ret) {
  502. mlog_errno(ret);
  503. return ret;
  504. }
  505. ret = ocfs2_prepare_refcount_change_for_del(inode,
  506. context->refcount_loc,
  507. phys_blkno,
  508. len,
  509. &credits,
  510. &extra_blocks);
  511. if (ret) {
  512. mlog_errno(ret);
  513. goto out;
  514. }
  515. }
  516. ret = ocfs2_lock_allocators_move_extents(inode, &context->et, len, 1,
  517. &context->meta_ac,
  518. NULL, extra_blocks, &credits);
  519. if (ret) {
  520. mlog_errno(ret);
  521. goto out;
  522. }
  523. /*
  524. * need to count 2 extra credits for global_bitmap inode and
  525. * group descriptor.
  526. */
  527. credits += OCFS2_INODE_UPDATE_CREDITS + 1;
  528. /*
  529. * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
  530. * logic, while we still need to lock the global_bitmap.
  531. */
  532. gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  533. OCFS2_INVALID_SLOT);
  534. if (!gb_inode) {
  535. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  536. ret = -EIO;
  537. goto out;
  538. }
  539. inode_lock(gb_inode);
  540. ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
  541. if (ret) {
  542. mlog_errno(ret);
  543. goto out_unlock_gb_mutex;
  544. }
  545. inode_lock(tl_inode);
  546. handle = ocfs2_start_trans(osb, credits);
  547. if (IS_ERR(handle)) {
  548. ret = PTR_ERR(handle);
  549. mlog_errno(ret);
  550. goto out_unlock_tl_inode;
  551. }
  552. new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
  553. ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
  554. GLOBAL_BITMAP_SYSTEM_INODE,
  555. OCFS2_INVALID_SLOT,
  556. &goal_bit, &gd_bh);
  557. if (ret) {
  558. mlog_errno(ret);
  559. goto out_commit;
  560. }
  561. /*
  562. * probe the victim cluster group to find a proper
  563. * region to fit wanted movement, it even will perfrom
  564. * a best-effort attempt by compromising to a threshold
  565. * around the goal.
  566. */
  567. ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
  568. new_phys_cpos);
  569. if (!*new_phys_cpos) {
  570. ret = -ENOSPC;
  571. goto out_commit;
  572. }
  573. ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
  574. *new_phys_cpos, ext_flags);
  575. if (ret) {
  576. mlog_errno(ret);
  577. goto out_commit;
  578. }
  579. gd = (struct ocfs2_group_desc *)gd_bh->b_data;
  580. ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
  581. le16_to_cpu(gd->bg_chain));
  582. if (ret) {
  583. mlog_errno(ret);
  584. goto out_commit;
  585. }
  586. ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
  587. goal_bit, len);
  588. if (ret) {
  589. ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len,
  590. le16_to_cpu(gd->bg_chain));
  591. mlog_errno(ret);
  592. }
  593. /*
  594. * Here we should write the new page out first if we are
  595. * in write-back mode.
  596. */
  597. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
  598. if (ret)
  599. mlog_errno(ret);
  600. out_commit:
  601. ocfs2_commit_trans(osb, handle);
  602. brelse(gd_bh);
  603. out_unlock_tl_inode:
  604. inode_unlock(tl_inode);
  605. ocfs2_inode_unlock(gb_inode, 1);
  606. out_unlock_gb_mutex:
  607. inode_unlock(gb_inode);
  608. brelse(gb_bh);
  609. iput(gb_inode);
  610. out:
  611. if (context->meta_ac) {
  612. ocfs2_free_alloc_context(context->meta_ac);
  613. context->meta_ac = NULL;
  614. }
  615. if (ref_tree)
  616. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  617. return ret;
  618. }
  619. /*
  620. * Helper to calculate the defraging length in one run according to threshold.
  621. */
  622. static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged,
  623. u32 threshold, int *skip)
  624. {
  625. if ((*alloc_size + *len_defraged) < threshold) {
  626. /*
  627. * proceed defragmentation until we meet the thresh
  628. */
  629. *len_defraged += *alloc_size;
  630. } else if (*len_defraged == 0) {
  631. /*
  632. * XXX: skip a large extent.
  633. */
  634. *skip = 1;
  635. } else {
  636. /*
  637. * split this extent to coalesce with former pieces as
  638. * to reach the threshold.
  639. *
  640. * we're done here with one cycle of defragmentation
  641. * in a size of 'thresh', resetting 'len_defraged'
  642. * forces a new defragmentation.
  643. */
  644. *alloc_size = threshold - *len_defraged;
  645. *len_defraged = 0;
  646. }
  647. }
  648. static int __ocfs2_move_extents_range(struct buffer_head *di_bh,
  649. struct ocfs2_move_extents_context *context)
  650. {
  651. int ret = 0, flags, do_defrag, skip = 0;
  652. u32 cpos, phys_cpos, move_start, len_to_move, alloc_size;
  653. u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0;
  654. struct inode *inode = context->inode;
  655. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  656. struct ocfs2_move_extents *range = context->range;
  657. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  658. if ((i_size_read(inode) == 0) || (range->me_len == 0))
  659. return 0;
  660. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  661. return 0;
  662. context->refcount_loc = le64_to_cpu(di->i_refcount_loc);
  663. ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh);
  664. ocfs2_init_dealloc_ctxt(&context->dealloc);
  665. /*
  666. * TO-DO XXX:
  667. *
  668. * - xattr extents.
  669. */
  670. do_defrag = context->auto_defrag;
  671. /*
  672. * extents moving happens in unit of clusters, for the sake
  673. * of simplicity, we may ignore two clusters where 'byte_start'
  674. * and 'byte_start + len' were within.
  675. */
  676. move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start);
  677. len_to_move = (range->me_start + range->me_len) >>
  678. osb->s_clustersize_bits;
  679. if (len_to_move >= move_start)
  680. len_to_move -= move_start;
  681. else
  682. len_to_move = 0;
  683. if (do_defrag) {
  684. defrag_thresh = range->me_threshold >> osb->s_clustersize_bits;
  685. if (defrag_thresh <= 1)
  686. goto done;
  687. } else
  688. new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  689. range->me_goal);
  690. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, "
  691. "thresh: %u\n",
  692. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  693. (unsigned long long)range->me_start,
  694. (unsigned long long)range->me_len,
  695. move_start, len_to_move, defrag_thresh);
  696. cpos = move_start;
  697. while (len_to_move) {
  698. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size,
  699. &flags);
  700. if (ret) {
  701. mlog_errno(ret);
  702. goto out;
  703. }
  704. if (alloc_size > len_to_move)
  705. alloc_size = len_to_move;
  706. /*
  707. * XXX: how to deal with a hole:
  708. *
  709. * - skip the hole of course
  710. * - force a new defragmentation
  711. */
  712. if (!phys_cpos) {
  713. if (do_defrag)
  714. len_defraged = 0;
  715. goto next;
  716. }
  717. if (do_defrag) {
  718. ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged,
  719. defrag_thresh, &skip);
  720. /*
  721. * skip large extents
  722. */
  723. if (skip) {
  724. skip = 0;
  725. goto next;
  726. }
  727. mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, "
  728. "alloc_size: %u, len_defraged: %u\n",
  729. cpos, phys_cpos, alloc_size, len_defraged);
  730. ret = ocfs2_defrag_extent(context, cpos, phys_cpos,
  731. &alloc_size, flags);
  732. } else {
  733. ret = ocfs2_move_extent(context, cpos, phys_cpos,
  734. &new_phys_cpos, alloc_size,
  735. flags);
  736. new_phys_cpos += alloc_size;
  737. }
  738. if (ret < 0) {
  739. mlog_errno(ret);
  740. goto out;
  741. }
  742. context->clusters_moved += alloc_size;
  743. next:
  744. cpos += alloc_size;
  745. len_to_move -= alloc_size;
  746. }
  747. done:
  748. range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE;
  749. out:
  750. range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb,
  751. context->clusters_moved);
  752. range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb,
  753. context->new_phys_cpos);
  754. ocfs2_schedule_truncate_log_flush(osb, 1);
  755. ocfs2_run_deallocs(osb, &context->dealloc);
  756. return ret;
  757. }
  758. static int ocfs2_move_extents(struct ocfs2_move_extents_context *context)
  759. {
  760. int status;
  761. handle_t *handle;
  762. struct inode *inode = context->inode;
  763. struct ocfs2_dinode *di;
  764. struct buffer_head *di_bh = NULL;
  765. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  766. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  767. return -EROFS;
  768. inode_lock(inode);
  769. /*
  770. * This prevents concurrent writes from other nodes
  771. */
  772. status = ocfs2_rw_lock(inode, 1);
  773. if (status) {
  774. mlog_errno(status);
  775. goto out;
  776. }
  777. status = ocfs2_inode_lock(inode, &di_bh, 1);
  778. if (status) {
  779. mlog_errno(status);
  780. goto out_rw_unlock;
  781. }
  782. /*
  783. * rememer ip_xattr_sem also needs to be held if necessary
  784. */
  785. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  786. status = __ocfs2_move_extents_range(di_bh, context);
  787. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  788. if (status) {
  789. mlog_errno(status);
  790. goto out_inode_unlock;
  791. }
  792. /*
  793. * We update ctime for these changes
  794. */
  795. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  796. if (IS_ERR(handle)) {
  797. status = PTR_ERR(handle);
  798. mlog_errno(status);
  799. goto out_inode_unlock;
  800. }
  801. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  802. OCFS2_JOURNAL_ACCESS_WRITE);
  803. if (status) {
  804. mlog_errno(status);
  805. goto out_commit;
  806. }
  807. di = (struct ocfs2_dinode *)di_bh->b_data;
  808. inode->i_ctime = current_time(inode);
  809. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  810. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  811. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  812. ocfs2_journal_dirty(handle, di_bh);
  813. out_commit:
  814. ocfs2_commit_trans(osb, handle);
  815. out_inode_unlock:
  816. brelse(di_bh);
  817. ocfs2_inode_unlock(inode, 1);
  818. out_rw_unlock:
  819. ocfs2_rw_unlock(inode, 1);
  820. out:
  821. inode_unlock(inode);
  822. return status;
  823. }
  824. int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
  825. {
  826. int status;
  827. struct inode *inode = file_inode(filp);
  828. struct ocfs2_move_extents range;
  829. struct ocfs2_move_extents_context *context;
  830. if (!argp)
  831. return -EINVAL;
  832. status = mnt_want_write_file(filp);
  833. if (status)
  834. return status;
  835. if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
  836. status = -EPERM;
  837. goto out_drop;
  838. }
  839. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  840. status = -EPERM;
  841. goto out_drop;
  842. }
  843. context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
  844. if (!context) {
  845. status = -ENOMEM;
  846. mlog_errno(status);
  847. goto out_drop;
  848. }
  849. context->inode = inode;
  850. context->file = filp;
  851. if (copy_from_user(&range, argp, sizeof(range))) {
  852. status = -EFAULT;
  853. goto out_free;
  854. }
  855. if (range.me_start > i_size_read(inode)) {
  856. status = -EINVAL;
  857. goto out_free;
  858. }
  859. if (range.me_start + range.me_len > i_size_read(inode))
  860. range.me_len = i_size_read(inode) - range.me_start;
  861. context->range = &range;
  862. if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
  863. context->auto_defrag = 1;
  864. /*
  865. * ok, the default theshold for the defragmentation
  866. * is 1M, since our maximum clustersize was 1M also.
  867. * any thought?
  868. */
  869. if (!range.me_threshold)
  870. range.me_threshold = 1024 * 1024;
  871. if (range.me_threshold > i_size_read(inode))
  872. range.me_threshold = i_size_read(inode);
  873. if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
  874. context->partial = 1;
  875. } else {
  876. /*
  877. * first best-effort attempt to validate and adjust the goal
  878. * (physical address in block), while it can't guarantee later
  879. * operation can succeed all the time since global_bitmap may
  880. * change a bit over time.
  881. */
  882. status = ocfs2_validate_and_adjust_move_goal(inode, &range);
  883. if (status)
  884. goto out_copy;
  885. }
  886. status = ocfs2_move_extents(context);
  887. if (status)
  888. mlog_errno(status);
  889. out_copy:
  890. /*
  891. * movement/defragmentation may end up being partially completed,
  892. * that's the reason why we need to return userspace the finished
  893. * length and new_offset even if failure happens somewhere.
  894. */
  895. if (copy_to_user(argp, &range, sizeof(range)))
  896. status = -EFAULT;
  897. out_free:
  898. kfree(context);
  899. out_drop:
  900. mnt_drop_write_file(filp);
  901. return status;
  902. }