extent_map.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * extent_map.c
  5. *
  6. * Block/Cluster mapping functions
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License, version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program; if not, write to the
  21. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 021110-1307, USA.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #define MLOG_MASK_PREFIX ML_EXTENT_MAP
  28. #include <cluster/masklog.h>
  29. #include "ocfs2.h"
  30. #include "alloc.h"
  31. #include "extent_map.h"
  32. #include "inode.h"
  33. #include "super.h"
  34. #include "buffer_head_io.h"
  35. /*
  36. * Return the index of the extent record which contains cluster #v_cluster.
  37. * -1 is returned if it was not found.
  38. *
  39. * Should work fine on interior and exterior nodes.
  40. */
  41. static int ocfs2_search_extent_list(struct ocfs2_extent_list *el,
  42. u32 v_cluster)
  43. {
  44. int ret = -1;
  45. int i;
  46. struct ocfs2_extent_rec *rec;
  47. u32 rec_end, rec_start;
  48. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  49. rec = &el->l_recs[i];
  50. rec_start = le32_to_cpu(rec->e_cpos);
  51. rec_end = rec_start + le32_to_cpu(rec->e_clusters);
  52. if (v_cluster >= rec_start && v_cluster < rec_end) {
  53. ret = i;
  54. break;
  55. }
  56. }
  57. return ret;
  58. }
  59. static int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  60. u32 *p_cluster, u32 *num_clusters)
  61. {
  62. int ret, i;
  63. struct buffer_head *di_bh = NULL;
  64. struct buffer_head *eb_bh = NULL;
  65. struct ocfs2_dinode *di;
  66. struct ocfs2_extent_block *eb;
  67. struct ocfs2_extent_list *el;
  68. struct ocfs2_extent_rec *rec;
  69. u32 coff;
  70. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  71. &di_bh, OCFS2_BH_CACHED, inode);
  72. if (ret) {
  73. mlog_errno(ret);
  74. goto out;
  75. }
  76. di = (struct ocfs2_dinode *) di_bh->b_data;
  77. el = &di->id2.i_list;
  78. if (el->l_tree_depth) {
  79. ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
  80. if (ret) {
  81. mlog_errno(ret);
  82. goto out;
  83. }
  84. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  85. el = &eb->h_list;
  86. }
  87. i = ocfs2_search_extent_list(el, v_cluster);
  88. if (i == -1) {
  89. /*
  90. * A hole was found. Return some canned values that
  91. * callers can key on.
  92. */
  93. *p_cluster = 0;
  94. if (num_clusters)
  95. *num_clusters = 1;
  96. } else {
  97. rec = &el->l_recs[i];
  98. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  99. if (!rec->e_blkno) {
  100. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  101. "record (%u, %u, 0)", inode->i_ino,
  102. le32_to_cpu(rec->e_cpos),
  103. le32_to_cpu(rec->e_clusters));
  104. ret = -EROFS;
  105. goto out;
  106. }
  107. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  108. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  109. le64_to_cpu(rec->e_blkno));
  110. *p_cluster = *p_cluster + coff;
  111. if (num_clusters)
  112. *num_clusters = le32_to_cpu(rec->e_clusters) - coff;
  113. }
  114. out:
  115. brelse(di_bh);
  116. brelse(eb_bh);
  117. return ret;
  118. }
  119. /*
  120. * This expects alloc_sem to be held. The allocation cannot change at
  121. * all while the map is in the process of being updated.
  122. */
  123. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  124. int *ret_count)
  125. {
  126. int ret;
  127. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  128. u32 cpos, num_clusters, p_cluster;
  129. u64 boff = 0;
  130. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  131. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters);
  132. if (ret) {
  133. mlog_errno(ret);
  134. goto out;
  135. }
  136. /*
  137. * p_cluster == 0 indicates a hole.
  138. */
  139. if (p_cluster) {
  140. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  141. boff += (v_blkno & (u64)(bpc - 1));
  142. }
  143. *p_blkno = boff;
  144. if (ret_count) {
  145. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  146. *ret_count -= v_blkno & (u64)(bpc - 1);
  147. }
  148. out:
  149. return ret;
  150. }