extent_map.c 4.5 KB
Newer Older
1 2 3 4 5
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * extent_map.c
 *
6
 * Block/Cluster mapping functions
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Copyright (C) 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License, version 2,  as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/types.h>

#define MLOG_MASK_PREFIX ML_EXTENT_MAP
#include <cluster/masklog.h>

#include "ocfs2.h"

34
#include "alloc.h"
35 36 37 38 39 40 41
#include "extent_map.h"
#include "inode.h"
#include "super.h"

#include "buffer_head_io.h"

/*
42 43
 * Return the index of the extent record which contains cluster #v_cluster.
 * -1 is returned if it was not found.
44
 *
45
 * Should work fine on interior and exterior nodes.
46
 */
47 48
static int ocfs2_search_extent_list(struct ocfs2_extent_list *el,
				    u32 v_cluster)
49
{
50 51
	int ret = -1;
	int i;
52
	struct ocfs2_extent_rec *rec;
53
	u32 rec_end, rec_start, clusters;
54

55
	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
56
		rec = &el->l_recs[i];
57

58
		rec_start = le32_to_cpu(rec->e_cpos);
59 60 61
		clusters = ocfs2_rec_clusters(el, rec);

		rec_end = rec_start + clusters;
62

63 64 65
		if (v_cluster >= rec_start && v_cluster < rec_end) {
			ret = i;
			break;
66 67 68 69 70 71
		}
	}

	return ret;
}

72
int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
73 74
		       u32 *p_cluster, u32 *num_clusters,
		       unsigned int *extent_flags)
75
{
76
	int ret, i;
77
	unsigned int flags = 0;
78 79
	struct buffer_head *di_bh = NULL;
	struct buffer_head *eb_bh = NULL;
80
	struct ocfs2_dinode *di;
81
	struct ocfs2_extent_block *eb;
82
	struct ocfs2_extent_list *el;
83 84
	struct ocfs2_extent_rec *rec;
	u32 coff;
85

86 87
	ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
			       &di_bh, OCFS2_BH_CACHED, inode);
88 89
	if (ret) {
		mlog_errno(ret);
90
		goto out;
91 92
	}

93 94
	di = (struct ocfs2_dinode *) di_bh->b_data;
	el = &di->id2.i_list;
95

96 97 98 99 100 101
	if (el->l_tree_depth) {
		ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
		if (ret) {
			mlog_errno(ret);
			goto out;
		}
102

103 104
		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
		el = &eb->h_list;
105 106 107 108 109 110 111 112 113

		if (el->l_tree_depth) {
			ocfs2_error(inode->i_sb,
				    "Inode %lu has non zero tree depth in "
				    "leaf block %llu\n", inode->i_ino,
				    (unsigned long long)eb_bh->b_blocknr);
			ret = -EROFS;
			goto out;
		}
114
	}
115

116 117
	i = ocfs2_search_extent_list(el, v_cluster);
	if (i == -1) {
118
		/*
119 120
		 * A hole was found. Return some canned values that
		 * callers can key on.
121
		 */
122 123 124 125 126
		*p_cluster = 0;
		if (num_clusters)
			*num_clusters = 1;
	} else {
		rec = &el->l_recs[i];
127

128
		BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
129

130 131 132 133
		if (!rec->e_blkno) {
			ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
				    "record (%u, %u, 0)", inode->i_ino,
				    le32_to_cpu(rec->e_cpos),
134
				    ocfs2_rec_clusters(el, rec));
135 136
			ret = -EROFS;
			goto out;
137 138
		}

139
		coff = v_cluster - le32_to_cpu(rec->e_cpos);
140

141 142 143
		*p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
						    le64_to_cpu(rec->e_blkno));
		*p_cluster = *p_cluster + coff;
144

145
		if (num_clusters)
146
			*num_clusters = ocfs2_rec_clusters(el, rec) - coff;
147 148

		flags = rec->e_flags;
149 150
	}

151 152 153
	if (extent_flags)
		*extent_flags = flags;

154 155 156
out:
	brelse(di_bh);
	brelse(eb_bh);
157 158 159 160
	return ret;
}

/*
161 162
 * This expects alloc_sem to be held. The allocation cannot change at
 * all while the map is in the process of being updated.
163
 */
164
int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
165
				int *ret_count, unsigned int *extent_flags)
166 167 168
{
	int ret;
	int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
169 170
	u32 cpos, num_clusters, p_cluster;
	u64 boff = 0;
171 172 173

	cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);

174 175
	ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
				 extent_flags);
176 177
	if (ret) {
		mlog_errno(ret);
178
		goto out;
179 180
	}

181 182 183 184 185
	/*
	 * p_cluster == 0 indicates a hole.
	 */
	if (p_cluster) {
		boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
186 187 188
		boff += (v_blkno & (u64)(bpc - 1));
	}

189
	*p_blkno = boff;
190

191 192 193
	if (ret_count) {
		*ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
		*ret_count -= v_blkno & (u64)(bpc - 1);
194 195
	}

196 197
out:
	return ret;
198
}