extent_map.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, clusters;
  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. clusters = ocfs2_rec_clusters(el, rec);
  52. rec_end = rec_start + clusters;
  53. if (v_cluster >= rec_start && v_cluster < rec_end) {
  54. ret = i;
  55. break;
  56. }
  57. }
  58. return ret;
  59. }
  60. int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  61. u32 *p_cluster, u32 *num_clusters)
  62. {
  63. int ret, i;
  64. struct buffer_head *di_bh = NULL;
  65. struct buffer_head *eb_bh = NULL;
  66. struct ocfs2_dinode *di;
  67. struct ocfs2_extent_block *eb;
  68. struct ocfs2_extent_list *el;
  69. struct ocfs2_extent_rec *rec;
  70. u32 coff;
  71. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  72. &di_bh, OCFS2_BH_CACHED, inode);
  73. if (ret) {
  74. mlog_errno(ret);
  75. goto out;
  76. }
  77. di = (struct ocfs2_dinode *) di_bh->b_data;
  78. el = &di->id2.i_list;
  79. if (el->l_tree_depth) {
  80. ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
  81. if (ret) {
  82. mlog_errno(ret);
  83. goto out;
  84. }
  85. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  86. el = &eb->h_list;
  87. if (el->l_tree_depth) {
  88. ocfs2_error(inode->i_sb,
  89. "Inode %lu has non zero tree depth in "
  90. "leaf block %llu\n", inode->i_ino,
  91. (unsigned long long)eb_bh->b_blocknr);
  92. ret = -EROFS;
  93. goto out;
  94. }
  95. }
  96. i = ocfs2_search_extent_list(el, v_cluster);
  97. if (i == -1) {
  98. /*
  99. * A hole was found. Return some canned values that
  100. * callers can key on.
  101. */
  102. *p_cluster = 0;
  103. if (num_clusters)
  104. *num_clusters = 1;
  105. } else {
  106. rec = &el->l_recs[i];
  107. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  108. if (!rec->e_blkno) {
  109. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  110. "record (%u, %u, 0)", inode->i_ino,
  111. le32_to_cpu(rec->e_cpos),
  112. ocfs2_rec_clusters(el, rec));
  113. ret = -EROFS;
  114. goto out;
  115. }
  116. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  117. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  118. le64_to_cpu(rec->e_blkno));
  119. *p_cluster = *p_cluster + coff;
  120. if (num_clusters)
  121. *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
  122. }
  123. out:
  124. brelse(di_bh);
  125. brelse(eb_bh);
  126. return ret;
  127. }
  128. /*
  129. * This expects alloc_sem to be held. The allocation cannot change at
  130. * all while the map is in the process of being updated.
  131. */
  132. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  133. int *ret_count)
  134. {
  135. int ret;
  136. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  137. u32 cpos, num_clusters, p_cluster;
  138. u64 boff = 0;
  139. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  140. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters);
  141. if (ret) {
  142. mlog_errno(ret);
  143. goto out;
  144. }
  145. /*
  146. * p_cluster == 0 indicates a hole.
  147. */
  148. if (p_cluster) {
  149. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  150. boff += (v_blkno & (u64)(bpc - 1));
  151. }
  152. *p_blkno = boff;
  153. if (ret_count) {
  154. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  155. *ret_count -= v_blkno & (u64)(bpc - 1);
  156. }
  157. out:
  158. return ret;
  159. }