move_extents.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /*
  54. * lock allocators, and reserving appropriate number of bits for
  55. * meta blocks and data clusters.
  56. *
  57. * in some cases, we don't need to reserve clusters, just let data_ac
  58. * be NULL.
  59. */
  60. static int ocfs2_lock_allocators_move_extents(struct inode *inode,
  61. struct ocfs2_extent_tree *et,
  62. u32 clusters_to_move,
  63. u32 extents_to_split,
  64. struct ocfs2_alloc_context **meta_ac,
  65. struct ocfs2_alloc_context **data_ac,
  66. int extra_blocks,
  67. int *credits)
  68. {
  69. int ret, num_free_extents;
  70. unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move;
  71. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  72. num_free_extents = ocfs2_num_free_extents(osb, et);
  73. if (num_free_extents < 0) {
  74. ret = num_free_extents;
  75. mlog_errno(ret);
  76. goto out;
  77. }
  78. if (!num_free_extents ||
  79. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
  80. extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
  81. ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac);
  82. if (ret) {
  83. mlog_errno(ret);
  84. goto out;
  85. }
  86. if (data_ac) {
  87. ret = ocfs2_reserve_clusters(osb, clusters_to_move, data_ac);
  88. if (ret) {
  89. mlog_errno(ret);
  90. goto out;
  91. }
  92. }
  93. *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el,
  94. clusters_to_move + 2);
  95. mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n",
  96. extra_blocks, clusters_to_move, *credits);
  97. out:
  98. if (ret) {
  99. if (*meta_ac) {
  100. ocfs2_free_alloc_context(*meta_ac);
  101. *meta_ac = NULL;
  102. }
  103. }
  104. return ret;
  105. }