move_extents.c 25 KB

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