1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Block/Cluster mapping functions
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
26 #include <linux/init.h>
27 #include <linux/types.h>
29 #define MLOG_MASK_PREFIX ML_EXTENT_MAP
30 #include <cluster/masklog.h>
35 #include "extent_map.h"
39 #include "buffer_head_io.h"
42 * Return the index of the extent record which contains cluster #v_cluster.
43 * -1 is returned if it was not found.
45 * Should work fine on interior and exterior nodes.
47 static int ocfs2_search_extent_list(struct ocfs2_extent_list *el,
52 struct ocfs2_extent_rec *rec;
53 u32 rec_end, rec_start, clusters;
55 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
58 rec_start = le32_to_cpu(rec->e_cpos);
59 clusters = ocfs2_rec_clusters(el, rec);
61 rec_end = rec_start + clusters;
63 if (v_cluster >= rec_start && v_cluster < rec_end) {
72 int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
73 u32 *p_cluster, u32 *num_clusters)
76 struct buffer_head *di_bh = NULL;
77 struct buffer_head *eb_bh = NULL;
78 struct ocfs2_dinode *di;
79 struct ocfs2_extent_block *eb;
80 struct ocfs2_extent_list *el;
81 struct ocfs2_extent_rec *rec;
84 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
85 &di_bh, OCFS2_BH_CACHED, inode);
91 di = (struct ocfs2_dinode *) di_bh->b_data;
94 if (el->l_tree_depth) {
95 ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
101 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
104 if (el->l_tree_depth) {
105 ocfs2_error(inode->i_sb,
106 "Inode %lu has non zero tree depth in "
107 "leaf block %llu\n", inode->i_ino,
108 (unsigned long long)eb_bh->b_blocknr);
114 i = ocfs2_search_extent_list(el, v_cluster);
117 * A hole was found. Return some canned values that
118 * callers can key on.
124 rec = &el->l_recs[i];
126 BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
129 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
130 "record (%u, %u, 0)", inode->i_ino,
131 le32_to_cpu(rec->e_cpos),
132 ocfs2_rec_clusters(el, rec));
137 coff = v_cluster - le32_to_cpu(rec->e_cpos);
139 *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
140 le64_to_cpu(rec->e_blkno));
141 *p_cluster = *p_cluster + coff;
144 *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
154 * This expects alloc_sem to be held. The allocation cannot change at
155 * all while the map is in the process of being updated.
157 int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
161 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
162 u32 cpos, num_clusters, p_cluster;
165 cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
167 ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters);
174 * p_cluster == 0 indicates a hole.
177 boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
178 boff += (v_blkno & (u64)(bpc - 1));
184 *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
185 *ret_count -= v_blkno & (u64)(bpc - 1);