blob: ea0ce41d4193130408baa98e4c6523dd1a78ba35 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * extent_map.c
5 *
Mark Fasheh363041a2007-01-17 12:31:35 -08006 * Block/Cluster mapping functions
Mark Fashehccd979b2005-12-15 14:31:24 -08007 *
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
25#include <linux/fs.h>
26#include <linux/init.h>
27#include <linux/types.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080028
29#define MLOG_MASK_PREFIX ML_EXTENT_MAP
30#include <cluster/masklog.h>
31
32#include "ocfs2.h"
33
Mark Fasheh363041a2007-01-17 12:31:35 -080034#include "alloc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080035#include "extent_map.h"
36#include "inode.h"
37#include "super.h"
38
39#include "buffer_head_io.h"
40
Mark Fashehccd979b2005-12-15 14:31:24 -080041/*
Mark Fasheh363041a2007-01-17 12:31:35 -080042 * Return the index of the extent record which contains cluster #v_cluster.
43 * -1 is returned if it was not found.
Mark Fashehccd979b2005-12-15 14:31:24 -080044 *
Mark Fasheh363041a2007-01-17 12:31:35 -080045 * Should work fine on interior and exterior nodes.
Mark Fashehccd979b2005-12-15 14:31:24 -080046 */
Mark Fasheh363041a2007-01-17 12:31:35 -080047static int ocfs2_search_extent_list(struct ocfs2_extent_list *el,
48 u32 v_cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -080049{
Mark Fasheh363041a2007-01-17 12:31:35 -080050 int ret = -1;
51 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -080052 struct ocfs2_extent_rec *rec;
Mark Fashehe48edee2007-03-07 16:46:57 -080053 u32 rec_end, rec_start, clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -080054
Mark Fasheh363041a2007-01-17 12:31:35 -080055 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -080056 rec = &el->l_recs[i];
Joel Becker110ba902006-02-28 17:58:36 -080057
Mark Fasheh363041a2007-01-17 12:31:35 -080058 rec_start = le32_to_cpu(rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -080059 clusters = ocfs2_rec_clusters(el, rec);
60
61 rec_end = rec_start + clusters;
Joel Becker110ba902006-02-28 17:58:36 -080062
Mark Fasheh363041a2007-01-17 12:31:35 -080063 if (v_cluster >= rec_start && v_cluster < rec_end) {
64 ret = i;
65 break;
Mark Fashehccd979b2005-12-15 14:31:24 -080066 }
67 }
68
Mark Fashehccd979b2005-12-15 14:31:24 -080069 return ret;
70}
71
Mark Fasheh9517bac2007-02-09 20:24:12 -080072int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
73 u32 *p_cluster, u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -080074{
Mark Fasheh363041a2007-01-17 12:31:35 -080075 int ret, i;
76 struct buffer_head *di_bh = NULL;
77 struct buffer_head *eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -080078 struct ocfs2_dinode *di;
Mark Fasheh363041a2007-01-17 12:31:35 -080079 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -080080 struct ocfs2_extent_list *el;
Mark Fasheh363041a2007-01-17 12:31:35 -080081 struct ocfs2_extent_rec *rec;
82 u32 coff;
Mark Fashehccd979b2005-12-15 14:31:24 -080083
Mark Fasheh363041a2007-01-17 12:31:35 -080084 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
85 &di_bh, OCFS2_BH_CACHED, inode);
Mark Fashehccd979b2005-12-15 14:31:24 -080086 if (ret) {
87 mlog_errno(ret);
Mark Fasheh363041a2007-01-17 12:31:35 -080088 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -080089 }
90
Mark Fasheh363041a2007-01-17 12:31:35 -080091 di = (struct ocfs2_dinode *) di_bh->b_data;
92 el = &di->id2.i_list;
93
94 if (el->l_tree_depth) {
95 ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
96 if (ret) {
97 mlog_errno(ret);
98 goto out;
99 }
100
101 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
102 el = &eb->h_list;
Mark Fashehe48edee2007-03-07 16:46:57 -0800103
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);
109 ret = -EROFS;
110 goto out;
111 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800112 }
113
Mark Fasheh363041a2007-01-17 12:31:35 -0800114 i = ocfs2_search_extent_list(el, v_cluster);
115 if (i == -1) {
116 /*
117 * A hole was found. Return some canned values that
118 * callers can key on.
119 */
120 *p_cluster = 0;
121 if (num_clusters)
122 *num_clusters = 1;
123 } else {
124 rec = &el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -0800125
Mark Fasheh363041a2007-01-17 12:31:35 -0800126 BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
Mark Fashehccd979b2005-12-15 14:31:24 -0800127
Mark Fasheh363041a2007-01-17 12:31:35 -0800128 if (!rec->e_blkno) {
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),
Mark Fashehe48edee2007-03-07 16:46:57 -0800132 ocfs2_rec_clusters(el, rec));
Mark Fasheh363041a2007-01-17 12:31:35 -0800133 ret = -EROFS;
134 goto out;
135 }
136
137 coff = v_cluster - le32_to_cpu(rec->e_cpos);
138
139 *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
140 le64_to_cpu(rec->e_blkno));
141 *p_cluster = *p_cluster + coff;
142
143 if (num_clusters)
Mark Fashehe48edee2007-03-07 16:46:57 -0800144 *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
Mark Fasheh363041a2007-01-17 12:31:35 -0800145 }
146
147out:
148 brelse(di_bh);
149 brelse(eb_bh);
150 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800151}
152
153/*
Mark Fasheh363041a2007-01-17 12:31:35 -0800154 * This expects alloc_sem to be held. The allocation cannot change at
155 * all while the map is in the process of being updated.
Mark Fashehccd979b2005-12-15 14:31:24 -0800156 */
Mark Fasheh363041a2007-01-17 12:31:35 -0800157int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
158 int *ret_count)
Mark Fashehccd979b2005-12-15 14:31:24 -0800159{
160 int ret;
Mark Fasheh363041a2007-01-17 12:31:35 -0800161 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
162 u32 cpos, num_clusters, p_cluster;
163 u64 boff = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800164
Mark Fasheh363041a2007-01-17 12:31:35 -0800165 cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800166
Mark Fasheh363041a2007-01-17 12:31:35 -0800167 ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters);
168 if (ret) {
169 mlog_errno(ret);
170 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -0800171 }
172
173 /*
Mark Fasheh363041a2007-01-17 12:31:35 -0800174 * p_cluster == 0 indicates a hole.
Mark Fashehccd979b2005-12-15 14:31:24 -0800175 */
Mark Fasheh363041a2007-01-17 12:31:35 -0800176 if (p_cluster) {
177 boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
Mark Fashehccd979b2005-12-15 14:31:24 -0800178 boff += (v_blkno & (u64)(bpc - 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800179 }
180
Mark Fasheh363041a2007-01-17 12:31:35 -0800181 *p_blkno = boff;
Mark Fashehccd979b2005-12-15 14:31:24 -0800182
Mark Fasheh363041a2007-01-17 12:31:35 -0800183 if (ret_count) {
184 *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
185 *ret_count -= v_blkno & (u64)(bpc - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800186 }
187
Mark Fasheh363041a2007-01-17 12:31:35 -0800188out:
189 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800190}