move_extents.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 || index >= le16_to_cpu(el->l_next_free_rec)) {
  91. ocfs2_error(inode->i_sb,
  92. "Inode %llu has an extent at cpos %u which can no "
  93. "longer be found.\n",
  94. (unsigned long long)ino, cpos);
  95. ret = -EROFS;
  96. goto out;
  97. }
  98. rec = &el->l_recs[index];
  99. BUG_ON(ext_flags != rec->e_flags);
  100. /*
  101. * after moving/defraging to new location, the extent is not going
  102. * to be refcounted anymore.
  103. */
  104. replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED;
  105. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  106. context->et.et_root_bh,
  107. OCFS2_JOURNAL_ACCESS_WRITE);
  108. if (ret) {
  109. mlog_errno(ret);
  110. goto out;
  111. }
  112. ret = ocfs2_split_extent(handle, &context->et, path, index,
  113. &replace_rec, context->meta_ac,
  114. &context->dealloc);
  115. if (ret) {
  116. mlog_errno(ret);
  117. goto out;
  118. }
  119. ocfs2_journal_dirty(handle, context->et.et_root_bh);
  120. context->new_phys_cpos = new_p_cpos;
  121. /*
  122. * need I to append truncate log for old clusters?
  123. */
  124. if (old_blkno) {
  125. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  126. ret = ocfs2_decrease_refcount(inode, handle,
  127. ocfs2_blocks_to_clusters(osb->sb,
  128. old_blkno),
  129. len, context->meta_ac,
  130. &context->dealloc, 1);
  131. else
  132. ret = ocfs2_truncate_log_append(osb, handle,
  133. old_blkno, len);
  134. }
  135. out:
  136. ocfs2_free_path(path);
  137. return ret;
  138. }
  139. /*
  140. * lock allocators, and reserving appropriate number of bits for
  141. * meta blocks and data clusters.
  142. *
  143. * in some cases, we don't need to reserve clusters, just let data_ac
  144. * be NULL.
  145. */
  146. static int ocfs2_lock_allocators_move_extents(struct inode *inode,
  147. struct ocfs2_extent_tree *et,
  148. u32 clusters_to_move,
  149. u32 extents_to_split,
  150. struct ocfs2_alloc_context **meta_ac,
  151. struct ocfs2_alloc_context **data_ac,
  152. int extra_blocks,
  153. int *credits)
  154. {
  155. int ret, num_free_extents;
  156. unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
  157. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  158. num_free_extents = ocfs2_num_free_extents(osb, et);
  159. if (num_free_extents < 0) {
  160. ret = num_free_extents;
  161. mlog_errno(ret);
  162. goto out;
  163. }
  164. if (!num_free_extents ||
  165. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
  166. extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
  167. ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
  168. if (ret) {
  169. mlog_errno(ret);
  170. goto out;
  171. }
  172. if (data_ac) {
  173. ret = ocfs2_reserve_clusters(osb, clusters_to_move, data_ac);
  174. if (ret) {
  175. mlog_errno(ret);
  176. goto out;
  177. }
  178. }
  179. *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el);
  180. mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
  181. extra_blocks, clusters_to_move, *credits);
  182. out:
  183. if (ret) {
  184. if (*meta_ac) {
  185. ocfs2_free_alloc_context(*meta_ac);
  186. *meta_ac = NULL;
  187. }
  188. }
  189. return ret;
  190. }
  191. /*
  192. * Using one journal handle to guarantee the data consistency in case
  193. * crash happens anywhere.
  194. *
  195. * XXX: defrag can end up with finishing partial extent as requested,
  196. * due to not enough contiguous clusters can be found in allocator.
  197. */
  198. static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
  199. u32 cpos, u32 phys_cpos, u32 *len, int ext_flags)
  200. {
  201. int ret, credits = 0, extra_blocks = 0, partial = context->partial;
  202. handle_t *handle;
  203. struct inode *inode = context->inode;
  204. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  205. struct inode *tl_inode = osb->osb_tl_inode;
  206. struct ocfs2_refcount_tree *ref_tree = NULL;
  207. u32 new_phys_cpos, new_len;
  208. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  209. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) {
  210. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
  211. OCFS2_HAS_REFCOUNT_FL));
  212. BUG_ON(!context->refcount_loc);
  213. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  214. &ref_tree, NULL);
  215. if (ret) {
  216. mlog_errno(ret);
  217. return ret;
  218. }
  219. ret = ocfs2_prepare_refcount_change_for_del(inode,
  220. context->refcount_loc,
  221. phys_blkno,
  222. *len,
  223. &credits,
  224. &extra_blocks);
  225. if (ret) {
  226. mlog_errno(ret);
  227. goto out;
  228. }
  229. }
  230. ret = ocfs2_lock_allocators_move_extents(inode, &context->et, *len, 1,
  231. &context->meta_ac,
  232. &context->data_ac,
  233. extra_blocks, &credits);
  234. if (ret) {
  235. mlog_errno(ret);
  236. goto out;
  237. }
  238. /*
  239. * should be using allocation reservation strategy there?
  240. *
  241. * if (context->data_ac)
  242. * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  243. */
  244. mutex_lock(&tl_inode->i_mutex);
  245. if (ocfs2_truncate_log_needs_flush(osb)) {
  246. ret = __ocfs2_flush_truncate_log(osb);
  247. if (ret < 0) {
  248. mlog_errno(ret);
  249. goto out_unlock_mutex;
  250. }
  251. }
  252. handle = ocfs2_start_trans(osb, credits);
  253. if (IS_ERR(handle)) {
  254. ret = PTR_ERR(handle);
  255. mlog_errno(ret);
  256. goto out_unlock_mutex;
  257. }
  258. ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len,
  259. &new_phys_cpos, &new_len);
  260. if (ret) {
  261. mlog_errno(ret);
  262. goto out_commit;
  263. }
  264. /*
  265. * allowing partial extent moving is kind of 'pros and cons', it makes
  266. * whole defragmentation less likely to fail, on the contrary, the bad
  267. * thing is it may make the fs even more fragmented after moving, let
  268. * userspace make a good decision here.
  269. */
  270. if (new_len != *len) {
  271. mlog(0, "len_claimed: %u, len: %u\n", new_len, *len);
  272. if (!partial) {
  273. context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
  274. ret = -ENOSPC;
  275. goto out_commit;
  276. }
  277. }
  278. mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
  279. phys_cpos, new_phys_cpos);
  280. ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos,
  281. new_phys_cpos, ext_flags);
  282. if (ret)
  283. mlog_errno(ret);
  284. if (partial && (new_len != *len))
  285. *len = new_len;
  286. /*
  287. * Here we should write the new page out first if we are
  288. * in write-back mode.
  289. */
  290. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len);
  291. if (ret)
  292. mlog_errno(ret);
  293. out_commit:
  294. ocfs2_commit_trans(osb, handle);
  295. out_unlock_mutex:
  296. mutex_unlock(&tl_inode->i_mutex);
  297. if (context->data_ac) {
  298. ocfs2_free_alloc_context(context->data_ac);
  299. context->data_ac = NULL;
  300. }
  301. if (context->meta_ac) {
  302. ocfs2_free_alloc_context(context->meta_ac);
  303. context->meta_ac = NULL;
  304. }
  305. out:
  306. if (ref_tree)
  307. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  308. return ret;
  309. }
  310. /*
  311. * find the victim alloc group, where #blkno fits.
  312. */
  313. static int ocfs2_find_victim_alloc_group(struct inode *inode,
  314. u64 vict_blkno,
  315. int type, int slot,
  316. int *vict_bit,
  317. struct buffer_head **ret_bh)
  318. {
  319. int ret, i, bits_per_unit = 0;
  320. u64 blkno;
  321. char namebuf[40];
  322. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  323. struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
  324. struct ocfs2_chain_list *cl;
  325. struct ocfs2_chain_rec *rec;
  326. struct ocfs2_dinode *ac_dinode;
  327. struct ocfs2_group_desc *bg;
  328. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
  329. ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
  330. strlen(namebuf), &blkno);
  331. if (ret) {
  332. ret = -ENOENT;
  333. goto out;
  334. }
  335. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
  336. if (ret) {
  337. mlog_errno(ret);
  338. goto out;
  339. }
  340. ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
  341. cl = &(ac_dinode->id2.i_chain);
  342. rec = &(cl->cl_recs[0]);
  343. if (type == GLOBAL_BITMAP_SYSTEM_INODE)
  344. bits_per_unit = osb->s_clustersize_bits -
  345. inode->i_sb->s_blocksize_bits;
  346. /*
  347. * 'vict_blkno' was out of the valid range.
  348. */
  349. if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
  350. (vict_blkno >= (le32_to_cpu(ac_dinode->id1.bitmap1.i_total) <<
  351. bits_per_unit))) {
  352. ret = -EINVAL;
  353. goto out;
  354. }
  355. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  356. rec = &(cl->cl_recs[i]);
  357. if (!rec)
  358. continue;
  359. bg = NULL;
  360. do {
  361. if (!bg)
  362. blkno = le64_to_cpu(rec->c_blkno);
  363. else
  364. blkno = le64_to_cpu(bg->bg_next_group);
  365. if (gd_bh) {
  366. brelse(gd_bh);
  367. gd_bh = NULL;
  368. }
  369. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
  370. if (ret) {
  371. mlog_errno(ret);
  372. goto out;
  373. }
  374. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  375. if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
  376. le16_to_cpu(bg->bg_bits))) {
  377. *ret_bh = gd_bh;
  378. *vict_bit = (vict_blkno - blkno) >>
  379. bits_per_unit;
  380. mlog(0, "find the victim group: #%llu, "
  381. "total_bits: %u, vict_bit: %u\n",
  382. blkno, le16_to_cpu(bg->bg_bits),
  383. *vict_bit);
  384. goto out;
  385. }
  386. } while (le64_to_cpu(bg->bg_next_group));
  387. }
  388. ret = -EINVAL;
  389. out:
  390. brelse(ac_bh);
  391. /*
  392. * caller has to release the gd_bh properly.
  393. */
  394. return ret;
  395. }
  396. /*
  397. * XXX: helper to validate and adjust moving goal.
  398. */
  399. static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
  400. struct ocfs2_move_extents *range)
  401. {
  402. int ret, goal_bit = 0;
  403. struct buffer_head *gd_bh = NULL;
  404. struct ocfs2_group_desc *bg;
  405. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  406. int c_to_b = 1 << (osb->s_clustersize_bits -
  407. inode->i_sb->s_blocksize_bits);
  408. /*
  409. * make goal become cluster aligned.
  410. */
  411. range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb,
  412. range->me_goal);
  413. /*
  414. * validate goal sits within global_bitmap, and return the victim
  415. * group desc
  416. */
  417. ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
  418. GLOBAL_BITMAP_SYSTEM_INODE,
  419. OCFS2_INVALID_SLOT,
  420. &goal_bit, &gd_bh);
  421. if (ret)
  422. goto out;
  423. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  424. /*
  425. * moving goal is not allowd to start with a group desc blok(#0 blk)
  426. * let's compromise to the latter cluster.
  427. */
  428. if (range->me_goal == le64_to_cpu(bg->bg_blkno))
  429. range->me_goal += c_to_b;
  430. /*
  431. * movement is not gonna cross two groups.
  432. */
  433. if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
  434. range->me_len) {
  435. ret = -EINVAL;
  436. goto out;
  437. }
  438. /*
  439. * more exact validations/adjustments will be performed later during
  440. * moving operation for each extent range.
  441. */
  442. mlog(0, "extents get ready to be moved to #%llu block\n",
  443. range->me_goal);
  444. out:
  445. brelse(gd_bh);
  446. return ret;
  447. }
  448. static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
  449. int *goal_bit, u32 move_len, u32 max_hop,
  450. u32 *phys_cpos)
  451. {
  452. int i, used, last_free_bits = 0, base_bit = *goal_bit;
  453. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  454. u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  455. le64_to_cpu(gd->bg_blkno));
  456. for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
  457. used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
  458. if (used) {
  459. /*
  460. * we even tried searching the free chunk by jumping
  461. * a 'max_hop' distance, but still failed.
  462. */
  463. if ((i - base_bit) > max_hop) {
  464. *phys_cpos = 0;
  465. break;
  466. }
  467. if (last_free_bits)
  468. last_free_bits = 0;
  469. continue;
  470. } else
  471. last_free_bits++;
  472. if (last_free_bits == move_len) {
  473. *goal_bit = i;
  474. *phys_cpos = base_cpos + i;
  475. break;
  476. }
  477. }
  478. mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
  479. }
  480. static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
  481. u32 cpos, u32 phys_cpos, u32 *new_phys_cpos,
  482. u32 len, int ext_flags)
  483. {
  484. int ret, credits = 0, extra_blocks = 0, goal_bit = 0;
  485. handle_t *handle;
  486. struct inode *inode = context->inode;
  487. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  488. struct inode *tl_inode = osb->osb_tl_inode;
  489. struct inode *gb_inode = NULL;
  490. struct buffer_head *gb_bh = NULL;
  491. struct buffer_head *gd_bh = NULL;
  492. struct ocfs2_group_desc *gd;
  493. struct ocfs2_refcount_tree *ref_tree = NULL;
  494. u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb,
  495. context->range->me_threshold);
  496. u64 phys_blkno, new_phys_blkno;
  497. phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  498. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
  499. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
  500. OCFS2_HAS_REFCOUNT_FL));
  501. BUG_ON(!context->refcount_loc);
  502. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  503. &ref_tree, NULL);
  504. if (ret) {
  505. mlog_errno(ret);
  506. return ret;
  507. }
  508. ret = ocfs2_prepare_refcount_change_for_del(inode,
  509. context->refcount_loc,
  510. phys_blkno,
  511. len,
  512. &credits,
  513. &extra_blocks);
  514. if (ret) {
  515. mlog_errno(ret);
  516. goto out;
  517. }
  518. }
  519. ret = ocfs2_lock_allocators_move_extents(inode, &context->et, len, 1,
  520. &context->meta_ac,
  521. NULL, extra_blocks, &credits);
  522. if (ret) {
  523. mlog_errno(ret);
  524. goto out;
  525. }
  526. /*
  527. * need to count 2 extra credits for global_bitmap inode and
  528. * group descriptor.
  529. */
  530. credits += OCFS2_INODE_UPDATE_CREDITS + 1;
  531. /*
  532. * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
  533. * logic, while we still need to lock the global_bitmap.
  534. */
  535. gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  536. OCFS2_INVALID_SLOT);
  537. if (!gb_inode) {
  538. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  539. ret = -EIO;
  540. goto out;
  541. }
  542. mutex_lock(&gb_inode->i_mutex);
  543. ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
  544. if (ret) {
  545. mlog_errno(ret);
  546. goto out_unlock_gb_mutex;
  547. }
  548. mutex_lock(&tl_inode->i_mutex);
  549. handle = ocfs2_start_trans(osb, credits);
  550. if (IS_ERR(handle)) {
  551. ret = PTR_ERR(handle);
  552. mlog_errno(ret);
  553. goto out_unlock_tl_inode;
  554. }
  555. new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
  556. ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno,
  557. GLOBAL_BITMAP_SYSTEM_INODE,
  558. OCFS2_INVALID_SLOT,
  559. &goal_bit, &gd_bh);
  560. if (ret) {
  561. mlog_errno(ret);
  562. goto out_commit;
  563. }
  564. /*
  565. * probe the victim cluster group to find a proper
  566. * region to fit wanted movement, it even will perfrom
  567. * a best-effort attempt by compromising to a threshold
  568. * around the goal.
  569. */
  570. ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop,
  571. new_phys_cpos);
  572. if (!*new_phys_cpos) {
  573. ret = -ENOSPC;
  574. goto out_commit;
  575. }
  576. ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
  577. *new_phys_cpos, ext_flags);
  578. if (ret) {
  579. mlog_errno(ret);
  580. goto out_commit;
  581. }
  582. gd = (struct ocfs2_group_desc *)gd_bh->b_data;
  583. ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len,
  584. le16_to_cpu(gd->bg_chain));
  585. if (ret) {
  586. mlog_errno(ret);
  587. goto out_commit;
  588. }
  589. ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh,
  590. goal_bit, len);
  591. if (ret)
  592. mlog_errno(ret);
  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. mutex_unlock(&tl_inode->i_mutex);
  605. ocfs2_inode_unlock(gb_inode, 1);
  606. out_unlock_gb_mutex:
  607. mutex_unlock(&gb_inode->i_mutex);
  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 (!inode)
  767. return -ENOENT;
  768. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  769. return -EROFS;
  770. mutex_lock(&inode->i_mutex);
  771. /*
  772. * This prevents concurrent writes from other nodes
  773. */
  774. status = ocfs2_rw_lock(inode, 1);
  775. if (status) {
  776. mlog_errno(status);
  777. goto out;
  778. }
  779. status = ocfs2_inode_lock(inode, &di_bh, 1);
  780. if (status) {
  781. mlog_errno(status);
  782. goto out_rw_unlock;
  783. }
  784. /*
  785. * rememer ip_xattr_sem also needs to be held if necessary
  786. */
  787. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  788. status = __ocfs2_move_extents_range(di_bh, context);
  789. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  790. if (status) {
  791. mlog_errno(status);
  792. goto out_inode_unlock;
  793. }
  794. /*
  795. * We update ctime for these changes
  796. */
  797. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  798. if (IS_ERR(handle)) {
  799. status = PTR_ERR(handle);
  800. mlog_errno(status);
  801. goto out_inode_unlock;
  802. }
  803. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  804. OCFS2_JOURNAL_ACCESS_WRITE);
  805. if (status) {
  806. mlog_errno(status);
  807. goto out_commit;
  808. }
  809. di = (struct ocfs2_dinode *)di_bh->b_data;
  810. inode->i_ctime = CURRENT_TIME;
  811. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  812. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  813. ocfs2_journal_dirty(handle, di_bh);
  814. out_commit:
  815. ocfs2_commit_trans(osb, handle);
  816. out_inode_unlock:
  817. brelse(di_bh);
  818. ocfs2_inode_unlock(inode, 1);
  819. out_rw_unlock:
  820. ocfs2_rw_unlock(inode, 1);
  821. out:
  822. mutex_unlock(&inode->i_mutex);
  823. return status;
  824. }
  825. int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp)
  826. {
  827. int status;
  828. struct inode *inode = file_inode(filp);
  829. struct ocfs2_move_extents range;
  830. struct ocfs2_move_extents_context *context;
  831. if (!argp)
  832. return -EINVAL;
  833. status = mnt_want_write_file(filp);
  834. if (status)
  835. return status;
  836. if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) {
  837. status = -EPERM;
  838. goto out_drop;
  839. }
  840. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  841. status = -EPERM;
  842. goto out_drop;
  843. }
  844. context = kzalloc(sizeof(struct ocfs2_move_extents_context), GFP_NOFS);
  845. if (!context) {
  846. status = -ENOMEM;
  847. mlog_errno(status);
  848. goto out_drop;
  849. }
  850. context->inode = inode;
  851. context->file = filp;
  852. if (copy_from_user(&range, argp, sizeof(range))) {
  853. status = -EFAULT;
  854. goto out_free;
  855. }
  856. if (range.me_start > i_size_read(inode)) {
  857. status = -EINVAL;
  858. goto out_free;
  859. }
  860. if (range.me_start + range.me_len > i_size_read(inode))
  861. range.me_len = i_size_read(inode) - range.me_start;
  862. context->range = &range;
  863. if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) {
  864. context->auto_defrag = 1;
  865. /*
  866. * ok, the default theshold for the defragmentation
  867. * is 1M, since our maximum clustersize was 1M also.
  868. * any thought?
  869. */
  870. if (!range.me_threshold)
  871. range.me_threshold = 1024 * 1024;
  872. if (range.me_threshold > i_size_read(inode))
  873. range.me_threshold = i_size_read(inode);
  874. if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG)
  875. context->partial = 1;
  876. } else {
  877. /*
  878. * first best-effort attempt to validate and adjust the goal
  879. * (physical address in block), while it can't guarantee later
  880. * operation can succeed all the time since global_bitmap may
  881. * change a bit over time.
  882. */
  883. status = ocfs2_validate_and_adjust_move_goal(inode, &range);
  884. if (status)
  885. goto out_copy;
  886. }
  887. status = ocfs2_move_extents(context);
  888. if (status)
  889. mlog_errno(status);
  890. out_copy:
  891. /*
  892. * movement/defragmentation may end up being partially completed,
  893. * that's the reason why we need to return userspace the finished
  894. * length and new_offset even if failure happens somewhere.
  895. */
  896. if (copy_to_user(argp, &range, sizeof(range)))
  897. status = -EFAULT;
  898. out_free:
  899. kfree(context);
  900. out_drop:
  901. mnt_drop_write_file(filp);
  902. return status;
  903. }