Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * extent_map.c |
| 5 | * |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 6 | * Block/Cluster mapping functions |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 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 | |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/types.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 28 | |
| 29 | #define MLOG_MASK_PREFIX ML_EXTENT_MAP |
| 30 | #include <cluster/masklog.h> |
| 31 | |
| 32 | #include "ocfs2.h" |
| 33 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 34 | #include "alloc.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 35 | #include "extent_map.h" |
| 36 | #include "inode.h" |
| 37 | #include "super.h" |
| 38 | |
| 39 | #include "buffer_head_io.h" |
| 40 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 41 | /* |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 42 | * Return the index of the extent record which contains cluster #v_cluster. |
| 43 | * -1 is returned if it was not found. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 44 | * |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 45 | * Should work fine on interior and exterior nodes. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 46 | */ |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 47 | static int ocfs2_search_extent_list(struct ocfs2_extent_list *el, |
| 48 | u32 v_cluster) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 49 | { |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 50 | int ret = -1; |
| 51 | int i; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 53 | u32 rec_end, rec_start, clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 54 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 55 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 56 | rec = &el->l_recs[i]; |
Joel Becker | 110ba90 | 2006-02-28 17:58:36 -0800 | [diff] [blame] | 57 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 58 | rec_start = le32_to_cpu(rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 59 | clusters = ocfs2_rec_clusters(el, rec); |
| 60 | |
| 61 | rec_end = rec_start + clusters; |
Joel Becker | 110ba90 | 2006-02-28 17:58:36 -0800 | [diff] [blame] | 62 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 63 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 64 | ret = i; |
| 65 | break; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 72 | int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 73 | u32 *p_cluster, u32 *num_clusters, |
| 74 | unsigned int *extent_flags) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 75 | { |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 76 | int ret, i; |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 77 | unsigned int flags = 0; |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 78 | struct buffer_head *di_bh = NULL; |
| 79 | struct buffer_head *eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 80 | struct ocfs2_dinode *di; |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 81 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 82 | struct ocfs2_extent_list *el; |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 83 | struct ocfs2_extent_rec *rec; |
| 84 | u32 coff; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 85 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 86 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno, |
| 87 | &di_bh, OCFS2_BH_CACHED, inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 88 | if (ret) { |
| 89 | mlog_errno(ret); |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 90 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 93 | di = (struct ocfs2_dinode *) di_bh->b_data; |
| 94 | el = &di->id2.i_list; |
| 95 | |
| 96 | if (el->l_tree_depth) { |
| 97 | ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh); |
| 98 | if (ret) { |
| 99 | mlog_errno(ret); |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 104 | el = &eb->h_list; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 105 | |
| 106 | if (el->l_tree_depth) { |
| 107 | ocfs2_error(inode->i_sb, |
| 108 | "Inode %lu has non zero tree depth in " |
| 109 | "leaf block %llu\n", inode->i_ino, |
| 110 | (unsigned long long)eb_bh->b_blocknr); |
| 111 | ret = -EROFS; |
| 112 | goto out; |
| 113 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 116 | i = ocfs2_search_extent_list(el, v_cluster); |
| 117 | if (i == -1) { |
| 118 | /* |
| 119 | * A hole was found. Return some canned values that |
| 120 | * callers can key on. |
| 121 | */ |
| 122 | *p_cluster = 0; |
| 123 | if (num_clusters) |
| 124 | *num_clusters = 1; |
| 125 | } else { |
| 126 | rec = &el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 127 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 128 | BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 129 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 130 | if (!rec->e_blkno) { |
| 131 | ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
| 132 | "record (%u, %u, 0)", inode->i_ino, |
| 133 | le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 134 | ocfs2_rec_clusters(el, rec)); |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 135 | ret = -EROFS; |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | coff = v_cluster - le32_to_cpu(rec->e_cpos); |
| 140 | |
| 141 | *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb, |
| 142 | le64_to_cpu(rec->e_blkno)); |
| 143 | *p_cluster = *p_cluster + coff; |
| 144 | |
| 145 | if (num_clusters) |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 146 | *num_clusters = ocfs2_rec_clusters(el, rec) - coff; |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 147 | |
| 148 | flags = rec->e_flags; |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 151 | if (extent_flags) |
| 152 | *extent_flags = flags; |
| 153 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 154 | out: |
| 155 | brelse(di_bh); |
| 156 | brelse(eb_bh); |
| 157 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 161 | * This expects alloc_sem to be held. The allocation cannot change at |
| 162 | * all while the map is in the process of being updated. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 163 | */ |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 164 | int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno, |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 165 | int *ret_count, unsigned int *extent_flags) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 166 | { |
| 167 | int ret; |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 168 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
| 169 | u32 cpos, num_clusters, p_cluster; |
| 170 | u64 boff = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 171 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 172 | cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 173 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 174 | ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters, |
| 175 | extent_flags); |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 176 | if (ret) { |
| 177 | mlog_errno(ret); |
| 178 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 182 | * p_cluster == 0 indicates a hole. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 183 | */ |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 184 | if (p_cluster) { |
| 185 | boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 186 | boff += (v_blkno & (u64)(bpc - 1)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 189 | *p_blkno = boff; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 190 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 191 | if (ret_count) { |
| 192 | *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters); |
| 193 | *ret_count -= v_blkno & (u64)(bpc - 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 196 | out: |
| 197 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 198 | } |