move_extents.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 "suballoc.h"
  37. #include "refcounttree.h"
  38. #include "move_extents.h"
  39. struct ocfs2_move_extents_context {
  40. struct inode *inode;
  41. struct file *file;
  42. int auto_defrag;
  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, context->file, 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. 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. clusters_to_move + 2);
  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. static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context,
  196. u32 cpos, u32 phys_cpos, u32 len, int ext_flags)
  197. {
  198. int ret, credits = 0, extra_blocks = 0;
  199. handle_t *handle;
  200. struct inode *inode = context->inode;
  201. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  202. struct inode *tl_inode = osb->osb_tl_inode;
  203. struct ocfs2_refcount_tree *ref_tree = NULL;
  204. u32 new_phys_cpos, new_len;
  205. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  206. if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) {
  207. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
  208. OCFS2_HAS_REFCOUNT_FL));
  209. BUG_ON(!context->refcount_loc);
  210. ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1,
  211. &ref_tree, NULL);
  212. if (ret) {
  213. mlog_errno(ret);
  214. return ret;
  215. }
  216. ret = ocfs2_prepare_refcount_change_for_del(inode,
  217. context->refcount_loc,
  218. phys_blkno,
  219. len,
  220. &credits,
  221. &extra_blocks);
  222. if (ret) {
  223. mlog_errno(ret);
  224. goto out;
  225. }
  226. }
  227. ret = ocfs2_lock_allocators_move_extents(inode, &context->et, len, 1,
  228. &context->meta_ac,
  229. &context->data_ac,
  230. extra_blocks, &credits);
  231. if (ret) {
  232. mlog_errno(ret);
  233. goto out;
  234. }
  235. /*
  236. * should be using allocation reservation strategy there?
  237. *
  238. * if (context->data_ac)
  239. * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  240. */
  241. mutex_lock(&tl_inode->i_mutex);
  242. if (ocfs2_truncate_log_needs_flush(osb)) {
  243. ret = __ocfs2_flush_truncate_log(osb);
  244. if (ret < 0) {
  245. mlog_errno(ret);
  246. goto out_unlock_mutex;
  247. }
  248. }
  249. handle = ocfs2_start_trans(osb, credits);
  250. if (IS_ERR(handle)) {
  251. ret = PTR_ERR(handle);
  252. mlog_errno(ret);
  253. goto out_unlock_mutex;
  254. }
  255. ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, len,
  256. &new_phys_cpos, &new_len);
  257. if (ret) {
  258. mlog_errno(ret);
  259. goto out_commit;
  260. }
  261. /*
  262. * we're not quite patient here to make multiple attempts for claiming
  263. * enough clusters, failure to claim clusters per-requested is not a
  264. * disaster though, it can only mean partial range of defragmentation
  265. * or extent movements gets gone, users anyway is able to have another
  266. * try as they wish anytime, since they're going to be returned a
  267. * '-ENOSPC' and completed length of this movement.
  268. */
  269. if (new_len != len) {
  270. mlog(0, "len_claimed: %u, len: %u\n", new_len, len);
  271. context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE;
  272. ret = -ENOSPC;
  273. goto out_commit;
  274. }
  275. mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos,
  276. phys_cpos, new_phys_cpos);
  277. ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos,
  278. new_phys_cpos, ext_flags);
  279. if (ret)
  280. mlog_errno(ret);
  281. /*
  282. * Here we should write the new page out first if we are
  283. * in write-back mode.
  284. */
  285. ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len);
  286. if (ret)
  287. mlog_errno(ret);
  288. out_commit:
  289. ocfs2_commit_trans(osb, handle);
  290. out_unlock_mutex:
  291. mutex_unlock(&tl_inode->i_mutex);
  292. if (context->data_ac) {
  293. ocfs2_free_alloc_context(context->data_ac);
  294. context->data_ac = NULL;
  295. }
  296. if (context->meta_ac) {
  297. ocfs2_free_alloc_context(context->meta_ac);
  298. context->meta_ac = NULL;
  299. }
  300. out:
  301. if (ref_tree)
  302. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  303. return ret;
  304. }
  305. /*
  306. * find the victim alloc group, where #blkno fits.
  307. */
  308. static int ocfs2_find_victim_alloc_group(struct inode *inode,
  309. u64 vict_blkno,
  310. int type, int slot,
  311. int *vict_bit,
  312. struct buffer_head **ret_bh)
  313. {
  314. int ret, i, blocks_per_unit = 1;
  315. u64 blkno;
  316. char namebuf[40];
  317. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  318. struct buffer_head *ac_bh = NULL, *gd_bh = NULL;
  319. struct ocfs2_chain_list *cl;
  320. struct ocfs2_chain_rec *rec;
  321. struct ocfs2_dinode *ac_dinode;
  322. struct ocfs2_group_desc *bg;
  323. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot);
  324. ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
  325. strlen(namebuf), &blkno);
  326. if (ret) {
  327. ret = -ENOENT;
  328. goto out;
  329. }
  330. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh);
  331. if (ret) {
  332. mlog_errno(ret);
  333. goto out;
  334. }
  335. ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data;
  336. cl = &(ac_dinode->id2.i_chain);
  337. rec = &(cl->cl_recs[0]);
  338. if (type == GLOBAL_BITMAP_SYSTEM_INODE)
  339. blocks_per_unit <<= (osb->s_clustersize_bits -
  340. inode->i_sb->s_blocksize_bits);
  341. /*
  342. * 'vict_blkno' was out of the valid range.
  343. */
  344. if ((vict_blkno < le64_to_cpu(rec->c_blkno)) ||
  345. (vict_blkno >= (le32_to_cpu(ac_dinode->id1.bitmap1.i_total) *
  346. blocks_per_unit))) {
  347. ret = -EINVAL;
  348. goto out;
  349. }
  350. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  351. rec = &(cl->cl_recs[i]);
  352. if (!rec)
  353. continue;
  354. bg = NULL;
  355. do {
  356. if (!bg)
  357. blkno = le64_to_cpu(rec->c_blkno);
  358. else
  359. blkno = le64_to_cpu(bg->bg_next_group);
  360. if (gd_bh) {
  361. brelse(gd_bh);
  362. gd_bh = NULL;
  363. }
  364. ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh);
  365. if (ret) {
  366. mlog_errno(ret);
  367. goto out;
  368. }
  369. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  370. if (vict_blkno < (le64_to_cpu(bg->bg_blkno) +
  371. le16_to_cpu(bg->bg_bits))) {
  372. *ret_bh = gd_bh;
  373. *vict_bit = (vict_blkno - blkno) /
  374. blocks_per_unit;
  375. mlog(0, "find the victim group: #%llu, "
  376. "total_bits: %u, vict_bit: %u\n",
  377. blkno, le16_to_cpu(bg->bg_bits),
  378. *vict_bit);
  379. goto out;
  380. }
  381. } while (le64_to_cpu(bg->bg_next_group));
  382. }
  383. ret = -EINVAL;
  384. out:
  385. brelse(ac_bh);
  386. /*
  387. * caller has to release the gd_bh properly.
  388. */
  389. return ret;
  390. }
  391. /*
  392. * XXX: helper to validate and adjust moving goal.
  393. */
  394. static int ocfs2_validate_and_adjust_move_goal(struct inode *inode,
  395. struct ocfs2_move_extents *range)
  396. {
  397. int ret, goal_bit = 0;
  398. struct buffer_head *gd_bh = NULL;
  399. struct ocfs2_group_desc *bg;
  400. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  401. int c_to_b = 1 << (osb->s_clustersize_bits -
  402. inode->i_sb->s_blocksize_bits);
  403. /*
  404. * validate goal sits within global_bitmap, and return the victim
  405. * group desc
  406. */
  407. ret = ocfs2_find_victim_alloc_group(inode, range->me_goal,
  408. GLOBAL_BITMAP_SYSTEM_INODE,
  409. OCFS2_INVALID_SLOT,
  410. &goal_bit, &gd_bh);
  411. if (ret)
  412. goto out;
  413. bg = (struct ocfs2_group_desc *)gd_bh->b_data;
  414. /*
  415. * make goal become cluster aligned.
  416. */
  417. if (range->me_goal % c_to_b)
  418. range->me_goal = range->me_goal / c_to_b * c_to_b;
  419. /*
  420. * moving goal is not allowd to start with a group desc blok(#0 blk)
  421. * let's compromise to the latter cluster.
  422. */
  423. if (range->me_goal == le64_to_cpu(bg->bg_blkno))
  424. range->me_goal += c_to_b;
  425. /*
  426. * movement is not gonna cross two groups.
  427. */
  428. if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize <
  429. range->me_len) {
  430. ret = -EINVAL;
  431. goto out;
  432. }
  433. /*
  434. * more exact validations/adjustments will be performed later during
  435. * moving operation for each extent range.
  436. */
  437. mlog(0, "extents get ready to be moved to #%llu block\n",
  438. range->me_goal);
  439. out:
  440. brelse(gd_bh);
  441. return ret;
  442. }
  443. static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh,
  444. int *goal_bit, u32 move_len, u32 max_hop,
  445. u32 *phys_cpos)
  446. {
  447. int i, used, last_free_bits = 0, base_bit = *goal_bit;
  448. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  449. u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  450. le64_to_cpu(gd->bg_blkno));
  451. for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) {
  452. used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap);
  453. if (used) {
  454. /*
  455. * we even tried searching the free chunk by jumping
  456. * a 'max_hop' distance, but still failed.
  457. */
  458. if ((i - base_bit) > max_hop) {
  459. *phys_cpos = 0;
  460. break;
  461. }
  462. if (last_free_bits)
  463. last_free_bits = 0;
  464. continue;
  465. } else
  466. last_free_bits++;
  467. if (last_free_bits == move_len) {
  468. *goal_bit = i;
  469. *phys_cpos = base_cpos + i;
  470. break;
  471. }
  472. }
  473. mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos);
  474. }
  475. static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  476. handle_t *handle,
  477. struct buffer_head *di_bh,
  478. u32 num_bits,
  479. u16 chain)
  480. {
  481. int ret;
  482. u32 tmp_used;
  483. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  484. struct ocfs2_chain_list *cl =
  485. (struct ocfs2_chain_list *) &di->id2.i_chain;
  486. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  487. OCFS2_JOURNAL_ACCESS_WRITE);
  488. if (ret < 0) {
  489. mlog_errno(ret);
  490. goto out;
  491. }
  492. tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  493. di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  494. le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
  495. ocfs2_journal_dirty(handle, di_bh);
  496. out:
  497. return ret;
  498. }
  499. static inline int ocfs2_block_group_set_bits(handle_t *handle,
  500. struct inode *alloc_inode,
  501. struct ocfs2_group_desc *bg,
  502. struct buffer_head *group_bh,
  503. unsigned int bit_off,
  504. unsigned int num_bits)
  505. {
  506. int status;
  507. void *bitmap = bg->bg_bitmap;
  508. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  509. /* All callers get the descriptor via
  510. * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
  511. BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  512. BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  513. mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
  514. num_bits);
  515. if (ocfs2_is_cluster_bitmap(alloc_inode))
  516. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  517. status = ocfs2_journal_access_gd(handle,
  518. INODE_CACHE(alloc_inode),
  519. group_bh,
  520. journal_type);
  521. if (status < 0) {
  522. mlog_errno(status);
  523. goto bail;
  524. }
  525. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  526. if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
  527. ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit"
  528. " count %u but claims %u are freed. num_bits %d",
  529. (unsigned long long)le64_to_cpu(bg->bg_blkno),
  530. le16_to_cpu(bg->bg_bits),
  531. le16_to_cpu(bg->bg_free_bits_count), num_bits);
  532. return -EROFS;
  533. }
  534. while (num_bits--)
  535. ocfs2_set_bit(bit_off++, bitmap);
  536. ocfs2_journal_dirty(handle, group_bh);
  537. bail:
  538. return status;
  539. }