Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com |
| 3 | * Written by Alex Tomas <alex@clusterfs.com> |
| 4 | * |
| 5 | * Architecture independence: |
| 6 | * Copyright (c) 2005, Bull S.A. |
| 7 | * Written by Pierre Peiffer <pierre.peiffer@bull.net> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public Licens |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Extents support for EXT4 |
| 25 | * |
| 26 | * TODO: |
| 27 | * - ext4*_error() should be used in some situations |
| 28 | * - analyze all BUG()/BUG_ON(), use -EIO where appropriate |
| 29 | * - smart tree reduction |
| 30 | */ |
| 31 | |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/time.h> |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 35 | #include <linux/jbd2.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 36 | #include <linux/highuid.h> |
| 37 | #include <linux/pagemap.h> |
| 38 | #include <linux/quotaops.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/slab.h> |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 41 | #include <linux/falloc.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 43 | #include <linux/fiemap.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 44 | #include "ext4_jbd2.h" |
| 45 | #include "ext4_extents.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 46 | |
| 47 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 48 | /* |
| 49 | * ext_pblock: |
| 50 | * combine low and high parts of physical block number into ext4_fsblk_t |
| 51 | */ |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 52 | ext4_fsblk_t ext_pblock(struct ext4_extent *ex) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 53 | { |
| 54 | ext4_fsblk_t block; |
| 55 | |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 56 | block = le32_to_cpu(ex->ee_start_lo); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 57 | block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 58 | return block; |
| 59 | } |
| 60 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 61 | /* |
| 62 | * idx_pblock: |
| 63 | * combine low and high parts of a leaf physical block number into ext4_fsblk_t |
| 64 | */ |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 65 | ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 66 | { |
| 67 | ext4_fsblk_t block; |
| 68 | |
Aneesh Kumar K.V | d8dd0b4 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 69 | block = le32_to_cpu(ix->ei_leaf_lo); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 70 | block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 71 | return block; |
| 72 | } |
| 73 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 74 | /* |
| 75 | * ext4_ext_store_pblock: |
| 76 | * stores a large physical block number into an extent struct, |
| 77 | * breaking it into parts |
| 78 | */ |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 79 | void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 80 | { |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 81 | ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 82 | ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 85 | /* |
| 86 | * ext4_idx_store_pblock: |
| 87 | * stores a large physical block number into an index struct, |
| 88 | * breaking it into parts |
| 89 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 90 | static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 91 | { |
Aneesh Kumar K.V | d8dd0b4 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 92 | ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 93 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 96 | static int ext4_ext_journal_restart(handle_t *handle, int needed) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 97 | { |
| 98 | int err; |
| 99 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 100 | if (!ext4_handle_valid(handle)) |
| 101 | return 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 102 | if (handle->h_buffer_credits > needed) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 103 | return 0; |
| 104 | err = ext4_journal_extend(handle, needed); |
Theodore Ts'o | 0123c93 | 2008-08-01 20:57:54 -0400 | [diff] [blame] | 105 | if (err <= 0) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 106 | return err; |
| 107 | return ext4_journal_restart(handle, needed); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * could return: |
| 112 | * - EROFS |
| 113 | * - ENOMEM |
| 114 | */ |
| 115 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 116 | struct ext4_ext_path *path) |
| 117 | { |
| 118 | if (path->p_bh) { |
| 119 | /* path points to block */ |
| 120 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 121 | } |
| 122 | /* path points to leaf/index in inode body */ |
| 123 | /* we use in-core data, no need to protect them */ |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * could return: |
| 129 | * - EROFS |
| 130 | * - ENOMEM |
| 131 | * - EIO |
| 132 | */ |
| 133 | static int ext4_ext_dirty(handle_t *handle, struct inode *inode, |
| 134 | struct ext4_ext_path *path) |
| 135 | { |
| 136 | int err; |
| 137 | if (path->p_bh) { |
| 138 | /* path points to block */ |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 139 | err = ext4_handle_dirty_metadata(handle, inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 140 | } else { |
| 141 | /* path points to leaf/index in inode body */ |
| 142 | err = ext4_mark_inode_dirty(handle, inode); |
| 143 | } |
| 144 | return err; |
| 145 | } |
| 146 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 147 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 148 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 149 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 150 | { |
| 151 | struct ext4_inode_info *ei = EXT4_I(inode); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 152 | ext4_fsblk_t bg_start; |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 153 | ext4_fsblk_t last_block; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 154 | ext4_grpblk_t colour; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 155 | ext4_group_t block_group; |
| 156 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 157 | int depth; |
| 158 | |
| 159 | if (path) { |
| 160 | struct ext4_extent *ex; |
| 161 | depth = path->p_depth; |
| 162 | |
| 163 | /* try to predict block placement */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 164 | ex = path[depth].p_ext; |
| 165 | if (ex) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 166 | return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 167 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 168 | /* it looks like index is empty; |
| 169 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 170 | if (path[depth].p_bh) |
| 171 | return path[depth].p_bh->b_blocknr; |
| 172 | } |
| 173 | |
| 174 | /* OK. use inode's group */ |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 175 | block_group = ei->i_block_group; |
| 176 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { |
| 177 | /* |
| 178 | * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME |
| 179 | * block groups per flexgroup, reserve the first block |
| 180 | * group for directories and special files. Regular |
| 181 | * files will start at the second block group. This |
| 182 | * tends to speed up directory access and improves |
| 183 | * fsck times. |
| 184 | */ |
| 185 | block_group &= ~(flex_size-1); |
| 186 | if (S_ISREG(inode->i_mode)) |
| 187 | block_group++; |
| 188 | } |
| 189 | bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 190 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 191 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
| 192 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 193 | /* |
| 194 | * If we are doing delayed allocation, we don't need take |
| 195 | * colour into account. |
| 196 | */ |
| 197 | if (test_opt(inode->i_sb, DELALLOC)) |
| 198 | return bg_start; |
| 199 | |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 200 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
| 201 | colour = (current->pid % 16) * |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 202 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 203 | else |
| 204 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 205 | return bg_start + colour + block; |
| 206 | } |
| 207 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 208 | /* |
| 209 | * Allocation for a meta data block |
| 210 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 211 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 212 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 213 | struct ext4_ext_path *path, |
| 214 | struct ext4_extent *ex, int *err) |
| 215 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 216 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 217 | |
| 218 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
Theodore Ts'o | 97df5d1 | 2008-12-12 12:41:28 -0500 | [diff] [blame] | 219 | newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 220 | return newblock; |
| 221 | } |
| 222 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 223 | static int ext4_ext_space_block(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 224 | { |
| 225 | int size; |
| 226 | |
| 227 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 228 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 229 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 230 | if (size > 6) |
| 231 | size = 6; |
| 232 | #endif |
| 233 | return size; |
| 234 | } |
| 235 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 236 | static int ext4_ext_space_block_idx(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 237 | { |
| 238 | int size; |
| 239 | |
| 240 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 241 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 242 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 243 | if (size > 5) |
| 244 | size = 5; |
| 245 | #endif |
| 246 | return size; |
| 247 | } |
| 248 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 249 | static int ext4_ext_space_root(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 250 | { |
| 251 | int size; |
| 252 | |
| 253 | size = sizeof(EXT4_I(inode)->i_data); |
| 254 | size -= sizeof(struct ext4_extent_header); |
| 255 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 256 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 257 | if (size > 3) |
| 258 | size = 3; |
| 259 | #endif |
| 260 | return size; |
| 261 | } |
| 262 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 263 | static int ext4_ext_space_root_idx(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 264 | { |
| 265 | int size; |
| 266 | |
| 267 | size = sizeof(EXT4_I(inode)->i_data); |
| 268 | size -= sizeof(struct ext4_extent_header); |
| 269 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 270 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 271 | if (size > 4) |
| 272 | size = 4; |
| 273 | #endif |
| 274 | return size; |
| 275 | } |
| 276 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 277 | /* |
| 278 | * Calculate the number of metadata blocks needed |
| 279 | * to allocate @blocks |
| 280 | * Worse case is one block per extent |
| 281 | */ |
| 282 | int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) |
| 283 | { |
| 284 | int lcap, icap, rcap, leafs, idxs, num; |
| 285 | int newextents = blocks; |
| 286 | |
| 287 | rcap = ext4_ext_space_root_idx(inode); |
| 288 | lcap = ext4_ext_space_block(inode); |
| 289 | icap = ext4_ext_space_block_idx(inode); |
| 290 | |
| 291 | /* number of new leaf blocks needed */ |
| 292 | num = leafs = (newextents + lcap - 1) / lcap; |
| 293 | |
| 294 | /* |
| 295 | * Worse case, we need separate index block(s) |
| 296 | * to link all new leaf blocks |
| 297 | */ |
| 298 | idxs = (leafs + icap - 1) / icap; |
| 299 | do { |
| 300 | num += idxs; |
| 301 | idxs = (idxs + icap - 1) / icap; |
| 302 | } while (idxs > rcap); |
| 303 | |
| 304 | return num; |
| 305 | } |
| 306 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 307 | static int |
| 308 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 309 | { |
| 310 | int max; |
| 311 | |
| 312 | if (depth == ext_depth(inode)) { |
| 313 | if (depth == 0) |
| 314 | max = ext4_ext_space_root(inode); |
| 315 | else |
| 316 | max = ext4_ext_space_root_idx(inode); |
| 317 | } else { |
| 318 | if (depth == 0) |
| 319 | max = ext4_ext_space_block(inode); |
| 320 | else |
| 321 | max = ext4_ext_space_block_idx(inode); |
| 322 | } |
| 323 | |
| 324 | return max; |
| 325 | } |
| 326 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 327 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 328 | { |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 329 | ext4_fsblk_t block = ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 330 | int len = ext4_ext_get_actual_len(ext); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 331 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 332 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int ext4_valid_extent_idx(struct inode *inode, |
| 336 | struct ext4_extent_idx *ext_idx) |
| 337 | { |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 338 | ext4_fsblk_t block = idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 339 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 340 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int ext4_valid_extent_entries(struct inode *inode, |
| 344 | struct ext4_extent_header *eh, |
| 345 | int depth) |
| 346 | { |
| 347 | struct ext4_extent *ext; |
| 348 | struct ext4_extent_idx *ext_idx; |
| 349 | unsigned short entries; |
| 350 | if (eh->eh_entries == 0) |
| 351 | return 1; |
| 352 | |
| 353 | entries = le16_to_cpu(eh->eh_entries); |
| 354 | |
| 355 | if (depth == 0) { |
| 356 | /* leaf entries */ |
| 357 | ext = EXT_FIRST_EXTENT(eh); |
| 358 | while (entries) { |
| 359 | if (!ext4_valid_extent(inode, ext)) |
| 360 | return 0; |
| 361 | ext++; |
| 362 | entries--; |
| 363 | } |
| 364 | } else { |
| 365 | ext_idx = EXT_FIRST_INDEX(eh); |
| 366 | while (entries) { |
| 367 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 368 | return 0; |
| 369 | ext_idx++; |
| 370 | entries--; |
| 371 | } |
| 372 | } |
| 373 | return 1; |
| 374 | } |
| 375 | |
| 376 | static int __ext4_ext_check(const char *function, struct inode *inode, |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 377 | struct ext4_extent_header *eh, |
| 378 | int depth) |
| 379 | { |
| 380 | const char *error_msg; |
| 381 | int max = 0; |
| 382 | |
| 383 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 384 | error_msg = "invalid magic"; |
| 385 | goto corrupted; |
| 386 | } |
| 387 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 388 | error_msg = "unexpected eh_depth"; |
| 389 | goto corrupted; |
| 390 | } |
| 391 | if (unlikely(eh->eh_max == 0)) { |
| 392 | error_msg = "invalid eh_max"; |
| 393 | goto corrupted; |
| 394 | } |
| 395 | max = ext4_ext_max_entries(inode, depth); |
| 396 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 397 | error_msg = "too large eh_max"; |
| 398 | goto corrupted; |
| 399 | } |
| 400 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 401 | error_msg = "invalid eh_entries"; |
| 402 | goto corrupted; |
| 403 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 404 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 405 | error_msg = "invalid extent entries"; |
| 406 | goto corrupted; |
| 407 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 408 | return 0; |
| 409 | |
| 410 | corrupted: |
| 411 | ext4_error(inode->i_sb, function, |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 412 | "bad header/extent in inode #%lu: %s - magic %x, " |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 413 | "entries %u, max %u(%u), depth %u(%u)", |
| 414 | inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), |
| 415 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 416 | max, le16_to_cpu(eh->eh_depth), depth); |
| 417 | |
| 418 | return -EIO; |
| 419 | } |
| 420 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 421 | #define ext4_ext_check(inode, eh, depth) \ |
| 422 | __ext4_ext_check(__func__, inode, eh, depth) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 423 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 424 | int ext4_ext_check_inode(struct inode *inode) |
| 425 | { |
| 426 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); |
| 427 | } |
| 428 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 429 | #ifdef EXT_DEBUG |
| 430 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 431 | { |
| 432 | int k, l = path->p_depth; |
| 433 | |
| 434 | ext_debug("path:"); |
| 435 | for (k = 0; k <= l; k++, path++) { |
| 436 | if (path->p_idx) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 437 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 438 | idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 439 | } else if (path->p_ext) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 440 | ext_debug(" %d:%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 441 | le32_to_cpu(path->p_ext->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 442 | ext4_ext_get_actual_len(path->p_ext), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 443 | ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 444 | } else |
| 445 | ext_debug(" []"); |
| 446 | } |
| 447 | ext_debug("\n"); |
| 448 | } |
| 449 | |
| 450 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 451 | { |
| 452 | int depth = ext_depth(inode); |
| 453 | struct ext4_extent_header *eh; |
| 454 | struct ext4_extent *ex; |
| 455 | int i; |
| 456 | |
| 457 | if (!path) |
| 458 | return; |
| 459 | |
| 460 | eh = path[depth].p_hdr; |
| 461 | ex = EXT_FIRST_EXTENT(eh); |
| 462 | |
| 463 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 464 | ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 465 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 466 | } |
| 467 | ext_debug("\n"); |
| 468 | } |
| 469 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 470 | #define ext4_ext_show_path(inode, path) |
| 471 | #define ext4_ext_show_leaf(inode, path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 472 | #endif |
| 473 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 474 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 475 | { |
| 476 | int depth = path->p_depth; |
| 477 | int i; |
| 478 | |
| 479 | for (i = 0; i <= depth; i++, path++) |
| 480 | if (path->p_bh) { |
| 481 | brelse(path->p_bh); |
| 482 | path->p_bh = NULL; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 487 | * ext4_ext_binsearch_idx: |
| 488 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 489 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 490 | */ |
| 491 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 492 | ext4_ext_binsearch_idx(struct inode *inode, |
| 493 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 494 | { |
| 495 | struct ext4_extent_header *eh = path->p_hdr; |
| 496 | struct ext4_extent_idx *r, *l, *m; |
| 497 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 498 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 499 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 500 | |
| 501 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 502 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 503 | while (l <= r) { |
| 504 | m = l + (r - l) / 2; |
| 505 | if (block < le32_to_cpu(m->ei_block)) |
| 506 | r = m - 1; |
| 507 | else |
| 508 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 509 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 510 | m, le32_to_cpu(m->ei_block), |
| 511 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | path->p_idx = l - 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 515 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 516 | idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 517 | |
| 518 | #ifdef CHECK_BINSEARCH |
| 519 | { |
| 520 | struct ext4_extent_idx *chix, *ix; |
| 521 | int k; |
| 522 | |
| 523 | chix = ix = EXT_FIRST_INDEX(eh); |
| 524 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
| 525 | if (k != 0 && |
| 526 | le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 527 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 528 | "first=0x%p\n", k, |
| 529 | ix, EXT_FIRST_INDEX(eh)); |
| 530 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 531 | le32_to_cpu(ix->ei_block), |
| 532 | le32_to_cpu(ix[-1].ei_block)); |
| 533 | } |
| 534 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 535 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 536 | if (block < le32_to_cpu(ix->ei_block)) |
| 537 | break; |
| 538 | chix = ix; |
| 539 | } |
| 540 | BUG_ON(chix != path->p_idx); |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | } |
| 545 | |
| 546 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 547 | * ext4_ext_binsearch: |
| 548 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 549 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 550 | */ |
| 551 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 552 | ext4_ext_binsearch(struct inode *inode, |
| 553 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 554 | { |
| 555 | struct ext4_extent_header *eh = path->p_hdr; |
| 556 | struct ext4_extent *r, *l, *m; |
| 557 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 558 | if (eh->eh_entries == 0) { |
| 559 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 560 | * this leaf is empty: |
| 561 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 562 | */ |
| 563 | return; |
| 564 | } |
| 565 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 566 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 567 | |
| 568 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 569 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 570 | |
| 571 | while (l <= r) { |
| 572 | m = l + (r - l) / 2; |
| 573 | if (block < le32_to_cpu(m->ee_block)) |
| 574 | r = m - 1; |
| 575 | else |
| 576 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 577 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 578 | m, le32_to_cpu(m->ee_block), |
| 579 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | path->p_ext = l - 1; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 583 | ext_debug(" -> %d:%llu:%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 584 | le32_to_cpu(path->p_ext->ee_block), |
| 585 | ext_pblock(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 586 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 587 | |
| 588 | #ifdef CHECK_BINSEARCH |
| 589 | { |
| 590 | struct ext4_extent *chex, *ex; |
| 591 | int k; |
| 592 | |
| 593 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 594 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 595 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 596 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 597 | if (block < le32_to_cpu(ex->ee_block)) |
| 598 | break; |
| 599 | chex = ex; |
| 600 | } |
| 601 | BUG_ON(chex != path->p_ext); |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | } |
| 606 | |
| 607 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 608 | { |
| 609 | struct ext4_extent_header *eh; |
| 610 | |
| 611 | eh = ext_inode_hdr(inode); |
| 612 | eh->eh_depth = 0; |
| 613 | eh->eh_entries = 0; |
| 614 | eh->eh_magic = EXT4_EXT_MAGIC; |
| 615 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); |
| 616 | ext4_mark_inode_dirty(handle, inode); |
| 617 | ext4_ext_invalidate_cache(inode); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | struct ext4_ext_path * |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 622 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, |
| 623 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 624 | { |
| 625 | struct ext4_extent_header *eh; |
| 626 | struct buffer_head *bh; |
| 627 | short int depth, i, ppos = 0, alloc = 0; |
| 628 | |
| 629 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 630 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 631 | |
| 632 | /* account possible depth increase */ |
| 633 | if (!path) { |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 634 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 635 | GFP_NOFS); |
| 636 | if (!path) |
| 637 | return ERR_PTR(-ENOMEM); |
| 638 | alloc = 1; |
| 639 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 640 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 641 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 642 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 643 | i = depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 644 | /* walk through the tree */ |
| 645 | while (i) { |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 646 | int need_to_validate = 0; |
| 647 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 648 | ext_debug("depth %d: num %d, max %d\n", |
| 649 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 650 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 651 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 652 | path[ppos].p_block = idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 653 | path[ppos].p_depth = i; |
| 654 | path[ppos].p_ext = NULL; |
| 655 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 656 | bh = sb_getblk(inode->i_sb, path[ppos].p_block); |
| 657 | if (unlikely(!bh)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 658 | goto err; |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 659 | if (!bh_uptodate_or_lock(bh)) { |
| 660 | if (bh_submit_read(bh) < 0) { |
| 661 | put_bh(bh); |
| 662 | goto err; |
| 663 | } |
| 664 | /* validate the extent entries */ |
| 665 | need_to_validate = 1; |
| 666 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 667 | eh = ext_block_hdr(bh); |
| 668 | ppos++; |
| 669 | BUG_ON(ppos > depth); |
| 670 | path[ppos].p_bh = bh; |
| 671 | path[ppos].p_hdr = eh; |
| 672 | i--; |
| 673 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 674 | if (need_to_validate && ext4_ext_check(inode, eh, i)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 675 | goto err; |
| 676 | } |
| 677 | |
| 678 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 679 | path[ppos].p_ext = NULL; |
| 680 | path[ppos].p_idx = NULL; |
| 681 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 682 | /* find extent */ |
| 683 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 684 | /* if not an empty leaf */ |
| 685 | if (path[ppos].p_ext) |
| 686 | path[ppos].p_block = ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 687 | |
| 688 | ext4_ext_show_path(inode, path); |
| 689 | |
| 690 | return path; |
| 691 | |
| 692 | err: |
| 693 | ext4_ext_drop_refs(path); |
| 694 | if (alloc) |
| 695 | kfree(path); |
| 696 | return ERR_PTR(-EIO); |
| 697 | } |
| 698 | |
| 699 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 700 | * ext4_ext_insert_index: |
| 701 | * insert new index [@logical;@ptr] into the block at @curp; |
| 702 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 703 | */ |
| 704 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 705 | struct ext4_ext_path *curp, |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 706 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 707 | { |
| 708 | struct ext4_extent_idx *ix; |
| 709 | int len, err; |
| 710 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 711 | err = ext4_ext_get_access(handle, inode, curp); |
| 712 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 713 | return err; |
| 714 | |
| 715 | BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); |
| 716 | len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; |
| 717 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 718 | /* insert after */ |
| 719 | if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { |
| 720 | len = (len - 1) * sizeof(struct ext4_extent_idx); |
| 721 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 722 | ext_debug("insert new index %d after: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 723 | "move %d from 0x%p to 0x%p\n", |
| 724 | logical, ptr, len, |
| 725 | (curp->p_idx + 1), (curp->p_idx + 2)); |
| 726 | memmove(curp->p_idx + 2, curp->p_idx + 1, len); |
| 727 | } |
| 728 | ix = curp->p_idx + 1; |
| 729 | } else { |
| 730 | /* insert before */ |
| 731 | len = len * sizeof(struct ext4_extent_idx); |
| 732 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 733 | ext_debug("insert new index %d before: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 734 | "move %d from 0x%p to 0x%p\n", |
| 735 | logical, ptr, len, |
| 736 | curp->p_idx, (curp->p_idx + 1)); |
| 737 | memmove(curp->p_idx + 1, curp->p_idx, len); |
| 738 | ix = curp->p_idx; |
| 739 | } |
| 740 | |
| 741 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 742 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 743 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 744 | |
| 745 | BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 746 | > le16_to_cpu(curp->p_hdr->eh_max)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 747 | BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); |
| 748 | |
| 749 | err = ext4_ext_dirty(handle, inode, curp); |
| 750 | ext4_std_error(inode->i_sb, err); |
| 751 | |
| 752 | return err; |
| 753 | } |
| 754 | |
| 755 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 756 | * ext4_ext_split: |
| 757 | * inserts new subtree into the path, using free index entry |
| 758 | * at depth @at: |
| 759 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 760 | * - makes decision where to split |
| 761 | * - moves remaining extents and index entries (right to the split point) |
| 762 | * into the newly allocated blocks |
| 763 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 764 | */ |
| 765 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
| 766 | struct ext4_ext_path *path, |
| 767 | struct ext4_extent *newext, int at) |
| 768 | { |
| 769 | struct buffer_head *bh = NULL; |
| 770 | int depth = ext_depth(inode); |
| 771 | struct ext4_extent_header *neh; |
| 772 | struct ext4_extent_idx *fidx; |
| 773 | struct ext4_extent *ex; |
| 774 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 775 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 776 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 777 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 778 | int err = 0; |
| 779 | |
| 780 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 781 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 782 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 783 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 784 | * border from split point */ |
| 785 | BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); |
| 786 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 787 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 788 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 789 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 790 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 791 | } else { |
| 792 | border = newext->ee_block; |
| 793 | ext_debug("leaf will be added." |
| 794 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 795 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 799 | * If error occurs, then we break processing |
| 800 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 801 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 802 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 803 | */ |
| 804 | |
| 805 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 806 | * Get array to track all allocated blocks. |
| 807 | * We need this to handle errors and free blocks |
| 808 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 809 | */ |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 810 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 811 | if (!ablocks) |
| 812 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 813 | |
| 814 | /* allocate all needed blocks */ |
| 815 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 816 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 817 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
| 818 | newext, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 819 | if (newblock == 0) |
| 820 | goto cleanup; |
| 821 | ablocks[a] = newblock; |
| 822 | } |
| 823 | |
| 824 | /* initialize new leaf */ |
| 825 | newblock = ablocks[--a]; |
| 826 | BUG_ON(newblock == 0); |
| 827 | bh = sb_getblk(inode->i_sb, newblock); |
| 828 | if (!bh) { |
| 829 | err = -EIO; |
| 830 | goto cleanup; |
| 831 | } |
| 832 | lock_buffer(bh); |
| 833 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 834 | err = ext4_journal_get_create_access(handle, bh); |
| 835 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 836 | goto cleanup; |
| 837 | |
| 838 | neh = ext_block_hdr(bh); |
| 839 | neh->eh_entries = 0; |
| 840 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); |
| 841 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 842 | neh->eh_depth = 0; |
| 843 | ex = EXT_FIRST_EXTENT(neh); |
| 844 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 845 | /* move remainder of path[depth] to the new leaf */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 846 | BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); |
| 847 | /* start copy from next extent */ |
| 848 | /* TODO: we could do it by single memmove */ |
| 849 | m = 0; |
| 850 | path[depth].p_ext++; |
| 851 | while (path[depth].p_ext <= |
| 852 | EXT_MAX_EXTENT(path[depth].p_hdr)) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 853 | ext_debug("move %d:%llu:%d in new leaf %llu\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 854 | le32_to_cpu(path[depth].p_ext->ee_block), |
| 855 | ext_pblock(path[depth].p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 856 | ext4_ext_get_actual_len(path[depth].p_ext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 857 | newblock); |
| 858 | /*memmove(ex++, path[depth].p_ext++, |
| 859 | sizeof(struct ext4_extent)); |
| 860 | neh->eh_entries++;*/ |
| 861 | path[depth].p_ext++; |
| 862 | m++; |
| 863 | } |
| 864 | if (m) { |
| 865 | memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 866 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | set_buffer_uptodate(bh); |
| 870 | unlock_buffer(bh); |
| 871 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 872 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 873 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 874 | goto cleanup; |
| 875 | brelse(bh); |
| 876 | bh = NULL; |
| 877 | |
| 878 | /* correct old leaf */ |
| 879 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 880 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 881 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 882 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 883 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 884 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 885 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 886 | goto cleanup; |
| 887 | |
| 888 | } |
| 889 | |
| 890 | /* create intermediate indexes */ |
| 891 | k = depth - at - 1; |
| 892 | BUG_ON(k < 0); |
| 893 | if (k) |
| 894 | ext_debug("create %d intermediate indices\n", k); |
| 895 | /* insert new index into current index block */ |
| 896 | /* current depth stored in i var */ |
| 897 | i = depth - 1; |
| 898 | while (k--) { |
| 899 | oldblock = newblock; |
| 900 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 901 | bh = sb_getblk(inode->i_sb, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 902 | if (!bh) { |
| 903 | err = -EIO; |
| 904 | goto cleanup; |
| 905 | } |
| 906 | lock_buffer(bh); |
| 907 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 908 | err = ext4_journal_get_create_access(handle, bh); |
| 909 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 910 | goto cleanup; |
| 911 | |
| 912 | neh = ext_block_hdr(bh); |
| 913 | neh->eh_entries = cpu_to_le16(1); |
| 914 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 915 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); |
| 916 | neh->eh_depth = cpu_to_le16(depth - i); |
| 917 | fidx = EXT_FIRST_INDEX(neh); |
| 918 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 919 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 920 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 921 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 922 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 923 | /* copy indexes */ |
| 924 | m = 0; |
| 925 | path[i].p_idx++; |
| 926 | |
| 927 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 928 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 929 | BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != |
| 930 | EXT_LAST_INDEX(path[i].p_hdr)); |
| 931 | while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 932 | ext_debug("%d: move %d:%llu in new index %llu\n", i, |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 933 | le32_to_cpu(path[i].p_idx->ei_block), |
| 934 | idx_pblock(path[i].p_idx), |
| 935 | newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 936 | /*memmove(++fidx, path[i].p_idx++, |
| 937 | sizeof(struct ext4_extent_idx)); |
| 938 | neh->eh_entries++; |
| 939 | BUG_ON(neh->eh_entries > neh->eh_max);*/ |
| 940 | path[i].p_idx++; |
| 941 | m++; |
| 942 | } |
| 943 | if (m) { |
| 944 | memmove(++fidx, path[i].p_idx - m, |
| 945 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 946 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 947 | } |
| 948 | set_buffer_uptodate(bh); |
| 949 | unlock_buffer(bh); |
| 950 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 951 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 952 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 953 | goto cleanup; |
| 954 | brelse(bh); |
| 955 | bh = NULL; |
| 956 | |
| 957 | /* correct old index */ |
| 958 | if (m) { |
| 959 | err = ext4_ext_get_access(handle, inode, path + i); |
| 960 | if (err) |
| 961 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 962 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 963 | err = ext4_ext_dirty(handle, inode, path + i); |
| 964 | if (err) |
| 965 | goto cleanup; |
| 966 | } |
| 967 | |
| 968 | i--; |
| 969 | } |
| 970 | |
| 971 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 972 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 973 | le32_to_cpu(border), newblock); |
| 974 | |
| 975 | cleanup: |
| 976 | if (bh) { |
| 977 | if (buffer_locked(bh)) |
| 978 | unlock_buffer(bh); |
| 979 | brelse(bh); |
| 980 | } |
| 981 | |
| 982 | if (err) { |
| 983 | /* free all allocated blocks in error case */ |
| 984 | for (i = 0; i < depth; i++) { |
| 985 | if (!ablocks[i]) |
| 986 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 987 | ext4_free_blocks(handle, inode, ablocks[i], 1, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | kfree(ablocks); |
| 991 | |
| 992 | return err; |
| 993 | } |
| 994 | |
| 995 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 996 | * ext4_ext_grow_indepth: |
| 997 | * implements tree growing procedure: |
| 998 | * - allocates new block |
| 999 | * - moves top-level data (index block or leaf) into the new block |
| 1000 | * - initializes new top-level, creating index that points to the |
| 1001 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1002 | */ |
| 1003 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
| 1004 | struct ext4_ext_path *path, |
| 1005 | struct ext4_extent *newext) |
| 1006 | { |
| 1007 | struct ext4_ext_path *curp = path; |
| 1008 | struct ext4_extent_header *neh; |
| 1009 | struct ext4_extent_idx *fidx; |
| 1010 | struct buffer_head *bh; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1011 | ext4_fsblk_t newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1012 | int err = 0; |
| 1013 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1014 | newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1015 | if (newblock == 0) |
| 1016 | return err; |
| 1017 | |
| 1018 | bh = sb_getblk(inode->i_sb, newblock); |
| 1019 | if (!bh) { |
| 1020 | err = -EIO; |
| 1021 | ext4_std_error(inode->i_sb, err); |
| 1022 | return err; |
| 1023 | } |
| 1024 | lock_buffer(bh); |
| 1025 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1026 | err = ext4_journal_get_create_access(handle, bh); |
| 1027 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1028 | unlock_buffer(bh); |
| 1029 | goto out; |
| 1030 | } |
| 1031 | |
| 1032 | /* move top-level index/leaf into new block */ |
| 1033 | memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); |
| 1034 | |
| 1035 | /* set size of new block */ |
| 1036 | neh = ext_block_hdr(bh); |
| 1037 | /* old root could have indexes or leaves |
| 1038 | * so calculate e_max right way */ |
| 1039 | if (ext_depth(inode)) |
| 1040 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); |
| 1041 | else |
| 1042 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); |
| 1043 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1044 | set_buffer_uptodate(bh); |
| 1045 | unlock_buffer(bh); |
| 1046 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1047 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1048 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1049 | goto out; |
| 1050 | |
| 1051 | /* create index in new top-level index: num,max,pointer */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1052 | err = ext4_ext_get_access(handle, inode, curp); |
| 1053 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1054 | goto out; |
| 1055 | |
| 1056 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; |
| 1057 | curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); |
| 1058 | curp->p_hdr->eh_entries = cpu_to_le16(1); |
| 1059 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 1060 | |
| 1061 | if (path[0].p_hdr->eh_depth) |
| 1062 | curp->p_idx->ei_block = |
| 1063 | EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; |
| 1064 | else |
| 1065 | curp->p_idx->ei_block = |
| 1066 | EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1067 | ext4_idx_store_pblock(curp->p_idx, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1068 | |
| 1069 | neh = ext_inode_hdr(inode); |
| 1070 | fidx = EXT_FIRST_INDEX(neh); |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1071 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1072 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1073 | le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1074 | |
| 1075 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); |
| 1076 | err = ext4_ext_dirty(handle, inode, curp); |
| 1077 | out: |
| 1078 | brelse(bh); |
| 1079 | |
| 1080 | return err; |
| 1081 | } |
| 1082 | |
| 1083 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1084 | * ext4_ext_create_new_leaf: |
| 1085 | * finds empty index and adds new leaf. |
| 1086 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1087 | */ |
| 1088 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
| 1089 | struct ext4_ext_path *path, |
| 1090 | struct ext4_extent *newext) |
| 1091 | { |
| 1092 | struct ext4_ext_path *curp; |
| 1093 | int depth, i, err = 0; |
| 1094 | |
| 1095 | repeat: |
| 1096 | i = depth = ext_depth(inode); |
| 1097 | |
| 1098 | /* walk up to the tree and look for free index entry */ |
| 1099 | curp = path + depth; |
| 1100 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1101 | i--; |
| 1102 | curp--; |
| 1103 | } |
| 1104 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1105 | /* we use already allocated block for index block, |
| 1106 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1107 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1108 | /* if we found index with free entry, then use that |
| 1109 | * entry: create all needed subtree and add new leaf */ |
| 1110 | err = ext4_ext_split(handle, inode, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1111 | if (err) |
| 1112 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1113 | |
| 1114 | /* refill path */ |
| 1115 | ext4_ext_drop_refs(path); |
| 1116 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1117 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1118 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1119 | if (IS_ERR(path)) |
| 1120 | err = PTR_ERR(path); |
| 1121 | } else { |
| 1122 | /* tree is full, time to grow in depth */ |
| 1123 | err = ext4_ext_grow_indepth(handle, inode, path, newext); |
| 1124 | if (err) |
| 1125 | goto out; |
| 1126 | |
| 1127 | /* refill path */ |
| 1128 | ext4_ext_drop_refs(path); |
| 1129 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1130 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1131 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1132 | if (IS_ERR(path)) { |
| 1133 | err = PTR_ERR(path); |
| 1134 | goto out; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1138 | * only first (depth 0 -> 1) produces free space; |
| 1139 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1140 | */ |
| 1141 | depth = ext_depth(inode); |
| 1142 | if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1143 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1144 | goto repeat; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | out: |
| 1149 | return err; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1153 | * search the closest allocated block to the left for *logical |
| 1154 | * and returns it at @logical + it's physical address at @phys |
| 1155 | * if *logical is the smallest allocated block, the function |
| 1156 | * returns 0 at @phys |
| 1157 | * return value contains 0 (success) or error code |
| 1158 | */ |
| 1159 | int |
| 1160 | ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, |
| 1161 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
| 1162 | { |
| 1163 | struct ext4_extent_idx *ix; |
| 1164 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1165 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1166 | |
| 1167 | BUG_ON(path == NULL); |
| 1168 | depth = path->p_depth; |
| 1169 | *phys = 0; |
| 1170 | |
| 1171 | if (depth == 0 && path->p_ext == NULL) |
| 1172 | return 0; |
| 1173 | |
| 1174 | /* usually extent in the path covers blocks smaller |
| 1175 | * then *logical, but it can be that extent is the |
| 1176 | * first one in the file */ |
| 1177 | |
| 1178 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1179 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1180 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1181 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); |
| 1182 | while (--depth >= 0) { |
| 1183 | ix = path[depth].p_idx; |
| 1184 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); |
| 1185 | } |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1189 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1190 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1191 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
| 1192 | *phys = ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * search the closest allocated block to the right for *logical |
| 1198 | * and returns it at @logical + it's physical address at @phys |
| 1199 | * if *logical is the smallest allocated block, the function |
| 1200 | * returns 0 at @phys |
| 1201 | * return value contains 0 (success) or error code |
| 1202 | */ |
| 1203 | int |
| 1204 | ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, |
| 1205 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
| 1206 | { |
| 1207 | struct buffer_head *bh = NULL; |
| 1208 | struct ext4_extent_header *eh; |
| 1209 | struct ext4_extent_idx *ix; |
| 1210 | struct ext4_extent *ex; |
| 1211 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1212 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1213 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1214 | |
| 1215 | BUG_ON(path == NULL); |
| 1216 | depth = path->p_depth; |
| 1217 | *phys = 0; |
| 1218 | |
| 1219 | if (depth == 0 && path->p_ext == NULL) |
| 1220 | return 0; |
| 1221 | |
| 1222 | /* usually extent in the path covers blocks smaller |
| 1223 | * then *logical, but it can be that extent is the |
| 1224 | * first one in the file */ |
| 1225 | |
| 1226 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1227 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1228 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1229 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); |
| 1230 | while (--depth >= 0) { |
| 1231 | ix = path[depth].p_idx; |
| 1232 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); |
| 1233 | } |
| 1234 | *logical = le32_to_cpu(ex->ee_block); |
| 1235 | *phys = ext_pblock(ex); |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1239 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1240 | |
| 1241 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1242 | /* next allocated block in this leaf */ |
| 1243 | ex++; |
| 1244 | *logical = le32_to_cpu(ex->ee_block); |
| 1245 | *phys = ext_pblock(ex); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* go up and search for index to the right */ |
| 1250 | while (--depth >= 0) { |
| 1251 | ix = path[depth].p_idx; |
| 1252 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1253 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1254 | } |
| 1255 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1256 | /* we've gone up to the root and found no index to the right */ |
| 1257 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1258 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1259 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1260 | /* we've found index to the right, let's |
| 1261 | * follow it and find the closest allocated |
| 1262 | * block to the right */ |
| 1263 | ix++; |
| 1264 | block = idx_pblock(ix); |
| 1265 | while (++depth < path->p_depth) { |
| 1266 | bh = sb_bread(inode->i_sb, block); |
| 1267 | if (bh == NULL) |
| 1268 | return -EIO; |
| 1269 | eh = ext_block_hdr(bh); |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1270 | /* subtract from p_depth to get proper eh_depth */ |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1271 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1272 | put_bh(bh); |
| 1273 | return -EIO; |
| 1274 | } |
| 1275 | ix = EXT_FIRST_INDEX(eh); |
| 1276 | block = idx_pblock(ix); |
| 1277 | put_bh(bh); |
| 1278 | } |
| 1279 | |
| 1280 | bh = sb_bread(inode->i_sb, block); |
| 1281 | if (bh == NULL) |
| 1282 | return -EIO; |
| 1283 | eh = ext_block_hdr(bh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1284 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1285 | put_bh(bh); |
| 1286 | return -EIO; |
| 1287 | } |
| 1288 | ex = EXT_FIRST_EXTENT(eh); |
| 1289 | *logical = le32_to_cpu(ex->ee_block); |
| 1290 | *phys = ext_pblock(ex); |
| 1291 | put_bh(bh); |
| 1292 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1296 | * ext4_ext_next_allocated_block: |
| 1297 | * returns allocated block in subsequent extent or EXT_MAX_BLOCK. |
| 1298 | * NOTE: it considers block number from index entry as |
| 1299 | * allocated block. Thus, index entries have to be consistent |
| 1300 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1301 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1302 | static ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1303 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1304 | { |
| 1305 | int depth; |
| 1306 | |
| 1307 | BUG_ON(path == NULL); |
| 1308 | depth = path->p_depth; |
| 1309 | |
| 1310 | if (depth == 0 && path->p_ext == NULL) |
| 1311 | return EXT_MAX_BLOCK; |
| 1312 | |
| 1313 | while (depth >= 0) { |
| 1314 | if (depth == path->p_depth) { |
| 1315 | /* leaf */ |
| 1316 | if (path[depth].p_ext != |
| 1317 | EXT_LAST_EXTENT(path[depth].p_hdr)) |
| 1318 | return le32_to_cpu(path[depth].p_ext[1].ee_block); |
| 1319 | } else { |
| 1320 | /* index */ |
| 1321 | if (path[depth].p_idx != |
| 1322 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1323 | return le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1324 | } |
| 1325 | depth--; |
| 1326 | } |
| 1327 | |
| 1328 | return EXT_MAX_BLOCK; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1332 | * ext4_ext_next_leaf_block: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1333 | * returns first allocated block from next leaf or EXT_MAX_BLOCK |
| 1334 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1335 | static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1336 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1337 | { |
| 1338 | int depth; |
| 1339 | |
| 1340 | BUG_ON(path == NULL); |
| 1341 | depth = path->p_depth; |
| 1342 | |
| 1343 | /* zero-tree has no leaf blocks at all */ |
| 1344 | if (depth == 0) |
| 1345 | return EXT_MAX_BLOCK; |
| 1346 | |
| 1347 | /* go to index block */ |
| 1348 | depth--; |
| 1349 | |
| 1350 | while (depth >= 0) { |
| 1351 | if (path[depth].p_idx != |
| 1352 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1353 | return (ext4_lblk_t) |
| 1354 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1355 | depth--; |
| 1356 | } |
| 1357 | |
| 1358 | return EXT_MAX_BLOCK; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1362 | * ext4_ext_correct_indexes: |
| 1363 | * if leaf gets modified and modified extent is first in the leaf, |
| 1364 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1365 | * TODO: do we need to correct tree in all cases? |
| 1366 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1367 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1368 | struct ext4_ext_path *path) |
| 1369 | { |
| 1370 | struct ext4_extent_header *eh; |
| 1371 | int depth = ext_depth(inode); |
| 1372 | struct ext4_extent *ex; |
| 1373 | __le32 border; |
| 1374 | int k, err = 0; |
| 1375 | |
| 1376 | eh = path[depth].p_hdr; |
| 1377 | ex = path[depth].p_ext; |
| 1378 | BUG_ON(ex == NULL); |
| 1379 | BUG_ON(eh == NULL); |
| 1380 | |
| 1381 | if (depth == 0) { |
| 1382 | /* there is no tree at all */ |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1387 | /* we correct tree if first leaf got modified only */ |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1392 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1393 | */ |
| 1394 | k = depth - 1; |
| 1395 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1396 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1397 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1398 | return err; |
| 1399 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1400 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1401 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1402 | return err; |
| 1403 | |
| 1404 | while (k--) { |
| 1405 | /* change all left-side indexes */ |
| 1406 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1407 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1408 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1409 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1410 | break; |
| 1411 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1412 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1413 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | return err; |
| 1418 | } |
| 1419 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 1420 | int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1421 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
| 1422 | struct ext4_extent *ex2) |
| 1423 | { |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1424 | unsigned short ext1_ee_len, ext2_ee_len, max_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1425 | |
| 1426 | /* |
| 1427 | * Make sure that either both extents are uninitialized, or |
| 1428 | * both are _not_. |
| 1429 | */ |
| 1430 | if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) |
| 1431 | return 0; |
| 1432 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1433 | if (ext4_ext_is_uninitialized(ex1)) |
| 1434 | max_len = EXT_UNINIT_MAX_LEN; |
| 1435 | else |
| 1436 | max_len = EXT_INIT_MAX_LEN; |
| 1437 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1438 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1439 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1440 | |
| 1441 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1442 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1443 | return 0; |
| 1444 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1445 | /* |
| 1446 | * To allow future support for preallocated extents to be added |
| 1447 | * as an RO_COMPAT feature, refuse to merge to extents if |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1448 | * this can result in the top bit of ee_len being set. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1449 | */ |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1450 | if (ext1_ee_len + ext2_ee_len > max_len) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1451 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1452 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1453 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1454 | return 0; |
| 1455 | #endif |
| 1456 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1457 | if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1458 | return 1; |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1463 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1464 | * It always tries to merge towards right. If you want to merge towards |
| 1465 | * left, pass "ex - 1" as argument instead of "ex". |
| 1466 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1467 | * 1 if they got merged. |
| 1468 | */ |
| 1469 | int ext4_ext_try_to_merge(struct inode *inode, |
| 1470 | struct ext4_ext_path *path, |
| 1471 | struct ext4_extent *ex) |
| 1472 | { |
| 1473 | struct ext4_extent_header *eh; |
| 1474 | unsigned int depth, len; |
| 1475 | int merge_done = 0; |
| 1476 | int uninitialized = 0; |
| 1477 | |
| 1478 | depth = ext_depth(inode); |
| 1479 | BUG_ON(path[depth].p_hdr == NULL); |
| 1480 | eh = path[depth].p_hdr; |
| 1481 | |
| 1482 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1483 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1484 | break; |
| 1485 | /* merge with next extent! */ |
| 1486 | if (ext4_ext_is_uninitialized(ex)) |
| 1487 | uninitialized = 1; |
| 1488 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1489 | + ext4_ext_get_actual_len(ex + 1)); |
| 1490 | if (uninitialized) |
| 1491 | ext4_ext_mark_uninitialized(ex); |
| 1492 | |
| 1493 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1494 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1495 | * sizeof(struct ext4_extent); |
| 1496 | memmove(ex + 1, ex + 2, len); |
| 1497 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1498 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1499 | merge_done = 1; |
| 1500 | WARN_ON(eh->eh_entries == 0); |
| 1501 | if (!eh->eh_entries) |
| 1502 | ext4_error(inode->i_sb, "ext4_ext_try_to_merge", |
| 1503 | "inode#%lu, eh->eh_entries = 0!", inode->i_ino); |
| 1504 | } |
| 1505 | |
| 1506 | return merge_done; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1510 | * check if a portion of the "newext" extent overlaps with an |
| 1511 | * existing extent. |
| 1512 | * |
| 1513 | * If there is an overlap discovered, it updates the length of the newext |
| 1514 | * such that there will be no overlap, and then returns 1. |
| 1515 | * If there is no overlap found, it returns 0. |
| 1516 | */ |
| 1517 | unsigned int ext4_ext_check_overlap(struct inode *inode, |
| 1518 | struct ext4_extent *newext, |
| 1519 | struct ext4_ext_path *path) |
| 1520 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1521 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1522 | unsigned int depth, len1; |
| 1523 | unsigned int ret = 0; |
| 1524 | |
| 1525 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1526 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1527 | depth = ext_depth(inode); |
| 1528 | if (!path[depth].p_ext) |
| 1529 | goto out; |
| 1530 | b2 = le32_to_cpu(path[depth].p_ext->ee_block); |
| 1531 | |
| 1532 | /* |
| 1533 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1534 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1535 | */ |
| 1536 | if (b2 < b1) { |
| 1537 | b2 = ext4_ext_next_allocated_block(path); |
| 1538 | if (b2 == EXT_MAX_BLOCK) |
| 1539 | goto out; |
| 1540 | } |
| 1541 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1542 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1543 | if (b1 + len1 < b1) { |
| 1544 | len1 = EXT_MAX_BLOCK - b1; |
| 1545 | newext->ee_len = cpu_to_le16(len1); |
| 1546 | ret = 1; |
| 1547 | } |
| 1548 | |
| 1549 | /* check for overlap */ |
| 1550 | if (b1 + len1 > b2) { |
| 1551 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1552 | ret = 1; |
| 1553 | } |
| 1554 | out: |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1559 | * ext4_ext_insert_extent: |
| 1560 | * tries to merge requsted extent into the existing extent or |
| 1561 | * inserts requested extent as new one into the tree, |
| 1562 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1563 | */ |
| 1564 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
| 1565 | struct ext4_ext_path *path, |
| 1566 | struct ext4_extent *newext) |
| 1567 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1568 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1569 | struct ext4_extent *ex, *fex; |
| 1570 | struct ext4_extent *nearex; /* nearest extent */ |
| 1571 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1572 | int depth, len, err; |
| 1573 | ext4_lblk_t next; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1574 | unsigned uninitialized = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1575 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1576 | BUG_ON(ext4_ext_get_actual_len(newext) == 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1577 | depth = ext_depth(inode); |
| 1578 | ex = path[depth].p_ext; |
| 1579 | BUG_ON(path[depth].p_hdr == NULL); |
| 1580 | |
| 1581 | /* try to insert block into found extent and return */ |
| 1582 | if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1583 | ext_debug("append %d block to %d:%d (from %llu)\n", |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1584 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1585 | le32_to_cpu(ex->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1586 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1587 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1588 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1589 | return err; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1590 | |
| 1591 | /* |
| 1592 | * ext4_can_extents_be_merged should have checked that either |
| 1593 | * both extents are uninitialized, or both aren't. Thus we |
| 1594 | * need to check only one of them here. |
| 1595 | */ |
| 1596 | if (ext4_ext_is_uninitialized(ex)) |
| 1597 | uninitialized = 1; |
| 1598 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1599 | + ext4_ext_get_actual_len(newext)); |
| 1600 | if (uninitialized) |
| 1601 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1602 | eh = path[depth].p_hdr; |
| 1603 | nearex = ex; |
| 1604 | goto merge; |
| 1605 | } |
| 1606 | |
| 1607 | repeat: |
| 1608 | depth = ext_depth(inode); |
| 1609 | eh = path[depth].p_hdr; |
| 1610 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 1611 | goto has_space; |
| 1612 | |
| 1613 | /* probably next leaf has space for us? */ |
| 1614 | fex = EXT_LAST_EXTENT(eh); |
| 1615 | next = ext4_ext_next_leaf_block(inode, path); |
| 1616 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) |
| 1617 | && next != EXT_MAX_BLOCK) { |
| 1618 | ext_debug("next leaf block - %d\n", next); |
| 1619 | BUG_ON(npath != NULL); |
| 1620 | npath = ext4_ext_find_extent(inode, next, NULL); |
| 1621 | if (IS_ERR(npath)) |
| 1622 | return PTR_ERR(npath); |
| 1623 | BUG_ON(npath->p_depth != path->p_depth); |
| 1624 | eh = npath[depth].p_hdr; |
| 1625 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
| 1626 | ext_debug("next leaf isnt full(%d)\n", |
| 1627 | le16_to_cpu(eh->eh_entries)); |
| 1628 | path = npath; |
| 1629 | goto repeat; |
| 1630 | } |
| 1631 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 1632 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 1633 | } |
| 1634 | |
| 1635 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1636 | * There is no free space in the found leaf. |
| 1637 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1638 | */ |
| 1639 | err = ext4_ext_create_new_leaf(handle, inode, path, newext); |
| 1640 | if (err) |
| 1641 | goto cleanup; |
| 1642 | depth = ext_depth(inode); |
| 1643 | eh = path[depth].p_hdr; |
| 1644 | |
| 1645 | has_space: |
| 1646 | nearex = path[depth].p_ext; |
| 1647 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1648 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1649 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1650 | goto cleanup; |
| 1651 | |
| 1652 | if (!nearex) { |
| 1653 | /* there is no extent in this leaf, create first one */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1654 | ext_debug("first extent in the leaf: %d:%llu:%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1655 | le32_to_cpu(newext->ee_block), |
| 1656 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1657 | ext4_ext_get_actual_len(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1658 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
| 1659 | } else if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1660 | > le32_to_cpu(nearex->ee_block)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1661 | /* BUG_ON(newext->ee_block == nearex->ee_block); */ |
| 1662 | if (nearex != EXT_LAST_EXTENT(eh)) { |
| 1663 | len = EXT_MAX_EXTENT(eh) - nearex; |
| 1664 | len = (len - 1) * sizeof(struct ext4_extent); |
| 1665 | len = len < 0 ? 0 : len; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1666 | ext_debug("insert %d:%llu:%d after: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1667 | "move %d from 0x%p to 0x%p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1668 | le32_to_cpu(newext->ee_block), |
| 1669 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1670 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1671 | nearex, len, nearex + 1, nearex + 2); |
| 1672 | memmove(nearex + 2, nearex + 1, len); |
| 1673 | } |
| 1674 | path[depth].p_ext = nearex + 1; |
| 1675 | } else { |
| 1676 | BUG_ON(newext->ee_block == nearex->ee_block); |
| 1677 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); |
| 1678 | len = len < 0 ? 0 : len; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1679 | ext_debug("insert %d:%llu:%d before: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1680 | "move %d from 0x%p to 0x%p\n", |
| 1681 | le32_to_cpu(newext->ee_block), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1682 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1683 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1684 | nearex, len, nearex + 1, nearex + 2); |
| 1685 | memmove(nearex + 1, nearex, len); |
| 1686 | path[depth].p_ext = nearex; |
| 1687 | } |
| 1688 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1689 | le16_add_cpu(&eh->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1690 | nearex = path[depth].p_ext; |
| 1691 | nearex->ee_block = newext->ee_block; |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1692 | ext4_ext_store_pblock(nearex, ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1693 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1694 | |
| 1695 | merge: |
| 1696 | /* try to merge extents to the right */ |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1697 | ext4_ext_try_to_merge(inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1698 | |
| 1699 | /* try to merge extents to the left */ |
| 1700 | |
| 1701 | /* time to correct all indexes above */ |
| 1702 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 1703 | if (err) |
| 1704 | goto cleanup; |
| 1705 | |
| 1706 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1707 | |
| 1708 | cleanup: |
| 1709 | if (npath) { |
| 1710 | ext4_ext_drop_refs(npath); |
| 1711 | kfree(npath); |
| 1712 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1713 | ext4_ext_invalidate_cache(inode); |
| 1714 | return err; |
| 1715 | } |
| 1716 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1717 | int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, |
| 1718 | ext4_lblk_t num, ext_prepare_callback func, |
| 1719 | void *cbdata) |
| 1720 | { |
| 1721 | struct ext4_ext_path *path = NULL; |
| 1722 | struct ext4_ext_cache cbex; |
| 1723 | struct ext4_extent *ex; |
| 1724 | ext4_lblk_t next, start = 0, end = 0; |
| 1725 | ext4_lblk_t last = block + num; |
| 1726 | int depth, exists, err = 0; |
| 1727 | |
| 1728 | BUG_ON(func == NULL); |
| 1729 | BUG_ON(inode == NULL); |
| 1730 | |
| 1731 | while (block < last && block != EXT_MAX_BLOCK) { |
| 1732 | num = last - block; |
| 1733 | /* find extent for this block */ |
| 1734 | path = ext4_ext_find_extent(inode, block, path); |
| 1735 | if (IS_ERR(path)) { |
| 1736 | err = PTR_ERR(path); |
| 1737 | path = NULL; |
| 1738 | break; |
| 1739 | } |
| 1740 | |
| 1741 | depth = ext_depth(inode); |
| 1742 | BUG_ON(path[depth].p_hdr == NULL); |
| 1743 | ex = path[depth].p_ext; |
| 1744 | next = ext4_ext_next_allocated_block(path); |
| 1745 | |
| 1746 | exists = 0; |
| 1747 | if (!ex) { |
| 1748 | /* there is no extent yet, so try to allocate |
| 1749 | * all requested space */ |
| 1750 | start = block; |
| 1751 | end = block + num; |
| 1752 | } else if (le32_to_cpu(ex->ee_block) > block) { |
| 1753 | /* need to allocate space before found extent */ |
| 1754 | start = block; |
| 1755 | end = le32_to_cpu(ex->ee_block); |
| 1756 | if (block + num < end) |
| 1757 | end = block + num; |
| 1758 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 1759 | + ext4_ext_get_actual_len(ex)) { |
| 1760 | /* need to allocate space after found extent */ |
| 1761 | start = block; |
| 1762 | end = block + num; |
| 1763 | if (end >= next) |
| 1764 | end = next; |
| 1765 | } else if (block >= le32_to_cpu(ex->ee_block)) { |
| 1766 | /* |
| 1767 | * some part of requested space is covered |
| 1768 | * by found extent |
| 1769 | */ |
| 1770 | start = block; |
| 1771 | end = le32_to_cpu(ex->ee_block) |
| 1772 | + ext4_ext_get_actual_len(ex); |
| 1773 | if (block + num < end) |
| 1774 | end = block + num; |
| 1775 | exists = 1; |
| 1776 | } else { |
| 1777 | BUG(); |
| 1778 | } |
| 1779 | BUG_ON(end <= start); |
| 1780 | |
| 1781 | if (!exists) { |
| 1782 | cbex.ec_block = start; |
| 1783 | cbex.ec_len = end - start; |
| 1784 | cbex.ec_start = 0; |
| 1785 | cbex.ec_type = EXT4_EXT_CACHE_GAP; |
| 1786 | } else { |
| 1787 | cbex.ec_block = le32_to_cpu(ex->ee_block); |
| 1788 | cbex.ec_len = ext4_ext_get_actual_len(ex); |
| 1789 | cbex.ec_start = ext_pblock(ex); |
| 1790 | cbex.ec_type = EXT4_EXT_CACHE_EXTENT; |
| 1791 | } |
| 1792 | |
| 1793 | BUG_ON(cbex.ec_len == 0); |
| 1794 | err = func(inode, path, &cbex, ex, cbdata); |
| 1795 | ext4_ext_drop_refs(path); |
| 1796 | |
| 1797 | if (err < 0) |
| 1798 | break; |
| 1799 | |
| 1800 | if (err == EXT_REPEAT) |
| 1801 | continue; |
| 1802 | else if (err == EXT_BREAK) { |
| 1803 | err = 0; |
| 1804 | break; |
| 1805 | } |
| 1806 | |
| 1807 | if (ext_depth(inode) != depth) { |
| 1808 | /* depth was changed. we have to realloc path */ |
| 1809 | kfree(path); |
| 1810 | path = NULL; |
| 1811 | } |
| 1812 | |
| 1813 | block = cbex.ec_block + cbex.ec_len; |
| 1814 | } |
| 1815 | |
| 1816 | if (path) { |
| 1817 | ext4_ext_drop_refs(path); |
| 1818 | kfree(path); |
| 1819 | } |
| 1820 | |
| 1821 | return err; |
| 1822 | } |
| 1823 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1824 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1825 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
Mingming Cao | dd54567 | 2007-07-31 00:37:46 -0700 | [diff] [blame] | 1826 | __u32 len, ext4_fsblk_t start, int type) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1827 | { |
| 1828 | struct ext4_ext_cache *cex; |
| 1829 | BUG_ON(len == 0); |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1830 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1831 | cex = &EXT4_I(inode)->i_cached_extent; |
| 1832 | cex->ec_type = type; |
| 1833 | cex->ec_block = block; |
| 1834 | cex->ec_len = len; |
| 1835 | cex->ec_start = start; |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1836 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1840 | * ext4_ext_put_gap_in_cache: |
| 1841 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1842 | * and cache this gap |
| 1843 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1844 | static void |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1845 | ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1846 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1847 | { |
| 1848 | int depth = ext_depth(inode); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1849 | unsigned long len; |
| 1850 | ext4_lblk_t lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1851 | struct ext4_extent *ex; |
| 1852 | |
| 1853 | ex = path[depth].p_ext; |
| 1854 | if (ex == NULL) { |
| 1855 | /* there is no extent yet, so gap is [0;-] */ |
| 1856 | lblock = 0; |
| 1857 | len = EXT_MAX_BLOCK; |
| 1858 | ext_debug("cache gap(whole file):"); |
| 1859 | } else if (block < le32_to_cpu(ex->ee_block)) { |
| 1860 | lblock = block; |
| 1861 | len = le32_to_cpu(ex->ee_block) - block; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1862 | ext_debug("cache gap(before): %u [%u:%u]", |
| 1863 | block, |
| 1864 | le32_to_cpu(ex->ee_block), |
| 1865 | ext4_ext_get_actual_len(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1866 | } else if (block >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1867 | + ext4_ext_get_actual_len(ex)) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1868 | ext4_lblk_t next; |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1869 | lblock = le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1870 | + ext4_ext_get_actual_len(ex); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1871 | |
| 1872 | next = ext4_ext_next_allocated_block(path); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1873 | ext_debug("cache gap(after): [%u:%u] %u", |
| 1874 | le32_to_cpu(ex->ee_block), |
| 1875 | ext4_ext_get_actual_len(ex), |
| 1876 | block); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1877 | BUG_ON(next == lblock); |
| 1878 | len = next - lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1879 | } else { |
| 1880 | lblock = len = 0; |
| 1881 | BUG(); |
| 1882 | } |
| 1883 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1884 | ext_debug(" -> %u:%lu\n", lblock, len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1885 | ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); |
| 1886 | } |
| 1887 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1888 | static int |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1889 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1890 | struct ext4_extent *ex) |
| 1891 | { |
| 1892 | struct ext4_ext_cache *cex; |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1893 | int ret = EXT4_EXT_CACHE_NO; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1894 | |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1895 | /* |
| 1896 | * We borrow i_block_reservation_lock to protect i_cached_extent |
| 1897 | */ |
| 1898 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1899 | cex = &EXT4_I(inode)->i_cached_extent; |
| 1900 | |
| 1901 | /* has cache valid data? */ |
| 1902 | if (cex->ec_type == EXT4_EXT_CACHE_NO) |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1903 | goto errout; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1904 | |
| 1905 | BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && |
| 1906 | cex->ec_type != EXT4_EXT_CACHE_EXTENT); |
| 1907 | if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1908 | ex->ee_block = cpu_to_le32(cex->ec_block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1909 | ext4_ext_store_pblock(ex, cex->ec_start); |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1910 | ex->ee_len = cpu_to_le16(cex->ec_len); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1911 | ext_debug("%u cached by %u:%u:%llu\n", |
| 1912 | block, |
| 1913 | cex->ec_block, cex->ec_len, cex->ec_start); |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1914 | ret = cex->ec_type; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1915 | } |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1916 | errout: |
| 1917 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 1918 | return ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1922 | * ext4_ext_rm_idx: |
| 1923 | * removes index from the index block. |
| 1924 | * It's used in truncate case only, thus all requests are for |
| 1925 | * last index in the block only. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1926 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1927 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1928 | struct ext4_ext_path *path) |
| 1929 | { |
| 1930 | struct buffer_head *bh; |
| 1931 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1932 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1933 | |
| 1934 | /* free index block */ |
| 1935 | path--; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1936 | leaf = idx_pblock(path->p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1937 | BUG_ON(path->p_hdr->eh_entries == 0); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1938 | err = ext4_ext_get_access(handle, inode, path); |
| 1939 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1940 | return err; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1941 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1942 | err = ext4_ext_dirty(handle, inode, path); |
| 1943 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1944 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1945 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1946 | bh = sb_find_get_block(inode->i_sb, leaf); |
| 1947 | ext4_forget(handle, 1, inode, bh, leaf); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1948 | ext4_free_blocks(handle, inode, leaf, 1, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1949 | return err; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1953 | * ext4_ext_calc_credits_for_single_extent: |
| 1954 | * This routine returns max. credits that needed to insert an extent |
| 1955 | * to the extent tree. |
| 1956 | * When pass the actual path, the caller should calculate credits |
| 1957 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1958 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1959 | int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1960 | struct ext4_ext_path *path) |
| 1961 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1962 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1963 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 1964 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1965 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1966 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1967 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1968 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 1969 | |
| 1970 | /* |
| 1971 | * There are some space in the leaf tree, no |
| 1972 | * need to account for leaf block credit |
| 1973 | * |
| 1974 | * bitmaps and block group descriptor blocks |
| 1975 | * and other metadat blocks still need to be |
| 1976 | * accounted. |
| 1977 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1978 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1979 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 1980 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1981 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1984 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1985 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1986 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1987 | /* |
| 1988 | * How many index/leaf blocks need to change/allocate to modify nrblocks? |
| 1989 | * |
| 1990 | * if nrblocks are fit in a single extent (chunk flag is 1), then |
| 1991 | * in the worse case, each tree level index/leaf need to be changed |
| 1992 | * if the tree split due to insert a new extent, then the old tree |
| 1993 | * index/leaf need to be updated too |
| 1994 | * |
| 1995 | * If the nrblocks are discontiguous, they could cause |
| 1996 | * the whole tree split more than once, but this is really rare. |
| 1997 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 1998 | int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 1999 | { |
| 2000 | int index; |
| 2001 | int depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2002 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2003 | if (chunk) |
| 2004 | index = depth * 2; |
| 2005 | else |
| 2006 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2007 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2008 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
| 2012 | struct ext4_extent *ex, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2013 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2014 | { |
| 2015 | struct buffer_head *bh; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2016 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2017 | int i, metadata = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2018 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2019 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2020 | metadata = 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2021 | #ifdef EXTENTS_STATS |
| 2022 | { |
| 2023 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2024 | spin_lock(&sbi->s_ext_stats_lock); |
| 2025 | sbi->s_ext_blocks += ee_len; |
| 2026 | sbi->s_ext_extents++; |
| 2027 | if (ee_len < sbi->s_ext_min) |
| 2028 | sbi->s_ext_min = ee_len; |
| 2029 | if (ee_len > sbi->s_ext_max) |
| 2030 | sbi->s_ext_max = ee_len; |
| 2031 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2032 | sbi->s_depth_max = ext_depth(inode); |
| 2033 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2034 | } |
| 2035 | #endif |
| 2036 | if (from >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2037 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2038 | /* tail removal */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2039 | ext4_lblk_t num; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2040 | ext4_fsblk_t start; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2041 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2042 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2043 | start = ext_pblock(ex) + ee_len - num; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2044 | ext_debug("free last %u blocks starting %llu\n", num, start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2045 | for (i = 0; i < num; i++) { |
| 2046 | bh = sb_find_get_block(inode->i_sb, start + i); |
| 2047 | ext4_forget(handle, 0, inode, bh, start + i); |
| 2048 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2049 | ext4_free_blocks(handle, inode, start, num, metadata); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2050 | } else if (from == le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2051 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2052 | printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2053 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2054 | } else { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2055 | printk(KERN_INFO "strange request: removal(2) " |
| 2056 | "%u-%u from %u:%u\n", |
| 2057 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2058 | } |
| 2059 | return 0; |
| 2060 | } |
| 2061 | |
| 2062 | static int |
| 2063 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2064 | struct ext4_ext_path *path, ext4_lblk_t start) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2065 | { |
| 2066 | int err = 0, correct_index = 0; |
| 2067 | int depth = ext_depth(inode), credits; |
| 2068 | struct ext4_extent_header *eh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2069 | ext4_lblk_t a, b, block; |
| 2070 | unsigned num; |
| 2071 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2072 | unsigned short ex_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2073 | unsigned uninitialized = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2074 | struct ext4_extent *ex; |
| 2075 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2076 | /* the header must be checked already in ext4_ext_remove_space() */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2077 | ext_debug("truncate since %u in leaf\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2078 | if (!path[depth].p_hdr) |
| 2079 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2080 | eh = path[depth].p_hdr; |
| 2081 | BUG_ON(eh == NULL); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2082 | |
| 2083 | /* find where to start removing */ |
| 2084 | ex = EXT_LAST_EXTENT(eh); |
| 2085 | |
| 2086 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2087 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2088 | |
| 2089 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2090 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2091 | |
| 2092 | if (ext4_ext_is_uninitialized(ex)) |
| 2093 | uninitialized = 1; |
| 2094 | else |
| 2095 | uninitialized = 0; |
| 2096 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2097 | ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); |
| 2098 | path[depth].p_ext = ex; |
| 2099 | |
| 2100 | a = ex_ee_block > start ? ex_ee_block : start; |
| 2101 | b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? |
| 2102 | ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; |
| 2103 | |
| 2104 | ext_debug(" border %u:%u\n", a, b); |
| 2105 | |
| 2106 | if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { |
| 2107 | block = 0; |
| 2108 | num = 0; |
| 2109 | BUG(); |
| 2110 | } else if (a != ex_ee_block) { |
| 2111 | /* remove tail of the extent */ |
| 2112 | block = ex_ee_block; |
| 2113 | num = a - block; |
| 2114 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
| 2115 | /* remove head of the extent */ |
| 2116 | block = a; |
| 2117 | num = b - a; |
| 2118 | /* there is no "make a hole" API yet */ |
| 2119 | BUG(); |
| 2120 | } else { |
| 2121 | /* remove whole extent: excellent! */ |
| 2122 | block = ex_ee_block; |
| 2123 | num = 0; |
| 2124 | BUG_ON(a != ex_ee_block); |
| 2125 | BUG_ON(b != ex_ee_block + ex_ee_len - 1); |
| 2126 | } |
| 2127 | |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2128 | /* |
| 2129 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2130 | * descriptor) for each block group; assume two block |
| 2131 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2132 | * the worst case |
| 2133 | */ |
| 2134 | credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2135 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2136 | correct_index = 1; |
| 2137 | credits += (ext_depth(inode)) + 1; |
| 2138 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2139 | credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2140 | |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2141 | err = ext4_ext_journal_restart(handle, credits); |
| 2142 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2143 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2144 | |
| 2145 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2146 | if (err) |
| 2147 | goto out; |
| 2148 | |
| 2149 | err = ext4_remove_blocks(handle, inode, ex, a, b); |
| 2150 | if (err) |
| 2151 | goto out; |
| 2152 | |
| 2153 | if (num == 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2154 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2155 | ext4_ext_store_pblock(ex, 0); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2156 | le16_add_cpu(&eh->eh_entries, -1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | ex->ee_block = cpu_to_le32(block); |
| 2160 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2161 | /* |
| 2162 | * Do not mark uninitialized if all the blocks in the |
| 2163 | * extent have been removed. |
| 2164 | */ |
| 2165 | if (uninitialized && num) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2166 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2167 | |
| 2168 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2169 | if (err) |
| 2170 | goto out; |
| 2171 | |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2172 | ext_debug("new extent: %u:%u:%llu\n", block, num, |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2173 | ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2174 | ex--; |
| 2175 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2176 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | if (correct_index && eh->eh_entries) |
| 2180 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2181 | |
| 2182 | /* if this leaf is free, then we should |
| 2183 | * remove it from index block above */ |
| 2184 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
| 2185 | err = ext4_ext_rm_idx(handle, inode, path + depth); |
| 2186 | |
| 2187 | out: |
| 2188 | return err; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2192 | * ext4_ext_more_to_rm: |
| 2193 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2194 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2195 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2196 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2197 | { |
| 2198 | BUG_ON(path->p_idx == NULL); |
| 2199 | |
| 2200 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2201 | return 0; |
| 2202 | |
| 2203 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2204 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2205 | * so we have to consider current index for truncation |
| 2206 | */ |
| 2207 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2208 | return 0; |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2212 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2213 | { |
| 2214 | struct super_block *sb = inode->i_sb; |
| 2215 | int depth = ext_depth(inode); |
| 2216 | struct ext4_ext_path *path; |
| 2217 | handle_t *handle; |
| 2218 | int i = 0, err = 0; |
| 2219 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2220 | ext_debug("truncate since %u\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2221 | |
| 2222 | /* probably first extent we're gonna free will be last in block */ |
| 2223 | handle = ext4_journal_start(inode, depth + 1); |
| 2224 | if (IS_ERR(handle)) |
| 2225 | return PTR_ERR(handle); |
| 2226 | |
| 2227 | ext4_ext_invalidate_cache(inode); |
| 2228 | |
| 2229 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2230 | * We start scanning from right side, freeing all the blocks |
| 2231 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2232 | */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 2233 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2234 | if (path == NULL) { |
| 2235 | ext4_journal_stop(handle); |
| 2236 | return -ENOMEM; |
| 2237 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2238 | path[0].p_hdr = ext_inode_hdr(inode); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2239 | if (ext4_ext_check(inode, path[0].p_hdr, depth)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2240 | err = -EIO; |
| 2241 | goto out; |
| 2242 | } |
| 2243 | path[0].p_depth = depth; |
| 2244 | |
| 2245 | while (i >= 0 && err == 0) { |
| 2246 | if (i == depth) { |
| 2247 | /* this is leaf block */ |
| 2248 | err = ext4_ext_rm_leaf(handle, inode, path, start); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2249 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2250 | brelse(path[i].p_bh); |
| 2251 | path[i].p_bh = NULL; |
| 2252 | i--; |
| 2253 | continue; |
| 2254 | } |
| 2255 | |
| 2256 | /* this is index block */ |
| 2257 | if (!path[i].p_hdr) { |
| 2258 | ext_debug("initialize header\n"); |
| 2259 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2262 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2263 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2264 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2265 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2266 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2267 | path[i].p_hdr, |
| 2268 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2269 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2270 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2271 | path[i].p_idx--; |
| 2272 | } |
| 2273 | |
| 2274 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2275 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2276 | path[i].p_idx); |
| 2277 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2278 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2279 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2280 | ext_debug("move to level %d (block %llu)\n", |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2281 | i + 1, idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2282 | memset(path + i + 1, 0, sizeof(*path)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2283 | bh = sb_bread(sb, idx_pblock(path[i].p_idx)); |
| 2284 | if (!bh) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2285 | /* should we reset i_size? */ |
| 2286 | err = -EIO; |
| 2287 | break; |
| 2288 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2289 | if (WARN_ON(i + 1 > depth)) { |
| 2290 | err = -EIO; |
| 2291 | break; |
| 2292 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2293 | if (ext4_ext_check(inode, ext_block_hdr(bh), |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2294 | depth - i - 1)) { |
| 2295 | err = -EIO; |
| 2296 | break; |
| 2297 | } |
| 2298 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2299 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2300 | /* save actual number of indexes since this |
| 2301 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2302 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2303 | i++; |
| 2304 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2305 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2306 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2307 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2308 | * handle must be already prepared by the |
| 2309 | * truncatei_leaf() */ |
| 2310 | err = ext4_ext_rm_idx(handle, inode, path + i); |
| 2311 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2312 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2313 | brelse(path[i].p_bh); |
| 2314 | path[i].p_bh = NULL; |
| 2315 | i--; |
| 2316 | ext_debug("return to level %d\n", i); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /* TODO: flexible tree reduction should be here */ |
| 2321 | if (path->p_hdr->eh_entries == 0) { |
| 2322 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2323 | * truncate to zero freed all the tree, |
| 2324 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2325 | */ |
| 2326 | err = ext4_ext_get_access(handle, inode, path); |
| 2327 | if (err == 0) { |
| 2328 | ext_inode_hdr(inode)->eh_depth = 0; |
| 2329 | ext_inode_hdr(inode)->eh_max = |
| 2330 | cpu_to_le16(ext4_ext_space_root(inode)); |
| 2331 | err = ext4_ext_dirty(handle, inode, path); |
| 2332 | } |
| 2333 | } |
| 2334 | out: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2335 | ext4_ext_drop_refs(path); |
| 2336 | kfree(path); |
| 2337 | ext4_journal_stop(handle); |
| 2338 | |
| 2339 | return err; |
| 2340 | } |
| 2341 | |
| 2342 | /* |
| 2343 | * called at mount time |
| 2344 | */ |
| 2345 | void ext4_ext_init(struct super_block *sb) |
| 2346 | { |
| 2347 | /* |
| 2348 | * possible initialization would be here |
| 2349 | */ |
| 2350 | |
Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2351 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2352 | printk(KERN_INFO "EXT4-fs: file extents enabled"); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 2353 | #ifdef AGGRESSIVE_TEST |
| 2354 | printk(", aggressive tests"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2355 | #endif |
| 2356 | #ifdef CHECK_BINSEARCH |
| 2357 | printk(", check binsearch"); |
| 2358 | #endif |
| 2359 | #ifdef EXTENTS_STATS |
| 2360 | printk(", stats"); |
| 2361 | #endif |
| 2362 | printk("\n"); |
| 2363 | #ifdef EXTENTS_STATS |
| 2364 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 2365 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 2366 | EXT4_SB(sb)->s_ext_max = 0; |
| 2367 | #endif |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | /* |
| 2372 | * called at umount time |
| 2373 | */ |
| 2374 | void ext4_ext_release(struct super_block *sb) |
| 2375 | { |
Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2376 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2377 | return; |
| 2378 | |
| 2379 | #ifdef EXTENTS_STATS |
| 2380 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 2381 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2382 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 2383 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 2384 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 2385 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 2386 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 2387 | } |
| 2388 | #endif |
| 2389 | } |
| 2390 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2391 | static void bi_complete(struct bio *bio, int error) |
| 2392 | { |
| 2393 | complete((struct completion *)bio->bi_private); |
| 2394 | } |
| 2395 | |
| 2396 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 2397 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 2398 | { |
| 2399 | int ret = -EIO; |
| 2400 | struct bio *bio; |
| 2401 | int blkbits, blocksize; |
| 2402 | sector_t ee_pblock; |
| 2403 | struct completion event; |
| 2404 | unsigned int ee_len, len, done, offset; |
| 2405 | |
| 2406 | |
| 2407 | blkbits = inode->i_blkbits; |
| 2408 | blocksize = inode->i_sb->s_blocksize; |
| 2409 | ee_len = ext4_ext_get_actual_len(ex); |
| 2410 | ee_pblock = ext_pblock(ex); |
| 2411 | |
| 2412 | /* convert ee_pblock to 512 byte sectors */ |
| 2413 | ee_pblock = ee_pblock << (blkbits - 9); |
| 2414 | |
| 2415 | while (ee_len > 0) { |
| 2416 | |
| 2417 | if (ee_len > BIO_MAX_PAGES) |
| 2418 | len = BIO_MAX_PAGES; |
| 2419 | else |
| 2420 | len = ee_len; |
| 2421 | |
| 2422 | bio = bio_alloc(GFP_NOIO, len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2423 | bio->bi_sector = ee_pblock; |
| 2424 | bio->bi_bdev = inode->i_sb->s_bdev; |
| 2425 | |
| 2426 | done = 0; |
| 2427 | offset = 0; |
| 2428 | while (done < len) { |
| 2429 | ret = bio_add_page(bio, ZERO_PAGE(0), |
| 2430 | blocksize, offset); |
| 2431 | if (ret != blocksize) { |
| 2432 | /* |
| 2433 | * We can't add any more pages because of |
| 2434 | * hardware limitations. Start a new bio. |
| 2435 | */ |
| 2436 | break; |
| 2437 | } |
| 2438 | done++; |
| 2439 | offset += blocksize; |
| 2440 | if (offset >= PAGE_CACHE_SIZE) |
| 2441 | offset = 0; |
| 2442 | } |
| 2443 | |
| 2444 | init_completion(&event); |
| 2445 | bio->bi_private = &event; |
| 2446 | bio->bi_end_io = bi_complete; |
| 2447 | submit_bio(WRITE, bio); |
| 2448 | wait_for_completion(&event); |
| 2449 | |
| 2450 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 2451 | ret = 0; |
| 2452 | else { |
| 2453 | ret = -EIO; |
| 2454 | break; |
| 2455 | } |
| 2456 | bio_put(bio); |
| 2457 | ee_len -= done; |
| 2458 | ee_pblock += done << (blkbits - 9); |
| 2459 | } |
| 2460 | return ret; |
| 2461 | } |
| 2462 | |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2463 | #define EXT4_EXT_ZERO_LEN 7 |
| 2464 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2465 | /* |
| 2466 | * This function is called by ext4_ext_get_blocks() if someone tries to write |
| 2467 | * to an uninitialized extent. It may result in splitting the uninitialized |
| 2468 | * extent into multiple extents (upto three - one initialized and two |
| 2469 | * uninitialized). |
| 2470 | * There are three possibilities: |
| 2471 | * a> There is no split required: Entire extent should be initialized |
| 2472 | * b> Splits in two extents: Write is happening at either end of the extent |
| 2473 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 2474 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2475 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
| 2476 | struct inode *inode, |
| 2477 | struct ext4_ext_path *path, |
| 2478 | ext4_lblk_t iblock, |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2479 | unsigned int max_blocks) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2480 | { |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2481 | struct ext4_extent *ex, newex, orig_ex; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2482 | struct ext4_extent *ex1 = NULL; |
| 2483 | struct ext4_extent *ex2 = NULL; |
| 2484 | struct ext4_extent *ex3 = NULL; |
| 2485 | struct ext4_extent_header *eh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2486 | ext4_lblk_t ee_block; |
| 2487 | unsigned int allocated, ee_len, depth; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2488 | ext4_fsblk_t newblock; |
| 2489 | int err = 0; |
| 2490 | int ret = 0; |
| 2491 | |
| 2492 | depth = ext_depth(inode); |
| 2493 | eh = path[depth].p_hdr; |
| 2494 | ex = path[depth].p_ext; |
| 2495 | ee_block = le32_to_cpu(ex->ee_block); |
| 2496 | ee_len = ext4_ext_get_actual_len(ex); |
| 2497 | allocated = ee_len - (iblock - ee_block); |
| 2498 | newblock = iblock - ee_block + ext_pblock(ex); |
| 2499 | ex2 = ex; |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2500 | orig_ex.ee_block = ex->ee_block; |
| 2501 | orig_ex.ee_len = cpu_to_le16(ee_len); |
| 2502 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2503 | |
Aneesh Kumar K.V | 9df5643 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2504 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2505 | if (err) |
| 2506 | goto out; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2507 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
| 2508 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { |
| 2509 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2510 | if (err) |
| 2511 | goto fix_extent_len; |
| 2512 | /* update the extent length and mark as initialized */ |
| 2513 | ex->ee_block = orig_ex.ee_block; |
| 2514 | ex->ee_len = orig_ex.ee_len; |
| 2515 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2516 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2517 | /* zeroed the full extent */ |
| 2518 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2519 | } |
Aneesh Kumar K.V | 9df5643 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2520 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2521 | /* ex1: ee_block to iblock - 1 : uninitialized */ |
| 2522 | if (iblock > ee_block) { |
| 2523 | ex1 = ex; |
| 2524 | ex1->ee_len = cpu_to_le16(iblock - ee_block); |
| 2525 | ext4_ext_mark_uninitialized(ex1); |
| 2526 | ex2 = &newex; |
| 2527 | } |
| 2528 | /* |
| 2529 | * for sanity, update the length of the ex2 extent before |
| 2530 | * we insert ex3, if ex1 is NULL. This is to avoid temporary |
| 2531 | * overlap of blocks. |
| 2532 | */ |
| 2533 | if (!ex1 && allocated > max_blocks) |
| 2534 | ex2->ee_len = cpu_to_le16(max_blocks); |
| 2535 | /* ex3: to ee_block + ee_len : uninitialised */ |
| 2536 | if (allocated > max_blocks) { |
| 2537 | unsigned int newdepth; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2538 | /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ |
| 2539 | if (allocated <= EXT4_EXT_ZERO_LEN) { |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2540 | /* |
| 2541 | * iblock == ee_block is handled by the zerouout |
| 2542 | * at the beginning. |
| 2543 | * Mark first half uninitialized. |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2544 | * Mark second half initialized and zero out the |
| 2545 | * initialized extent |
| 2546 | */ |
| 2547 | ex->ee_block = orig_ex.ee_block; |
| 2548 | ex->ee_len = cpu_to_le16(ee_len - allocated); |
| 2549 | ext4_ext_mark_uninitialized(ex); |
| 2550 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2551 | ext4_ext_dirty(handle, inode, path + depth); |
| 2552 | |
| 2553 | ex3 = &newex; |
| 2554 | ex3->ee_block = cpu_to_le32(iblock); |
| 2555 | ext4_ext_store_pblock(ex3, newblock); |
| 2556 | ex3->ee_len = cpu_to_le16(allocated); |
| 2557 | err = ext4_ext_insert_extent(handle, inode, path, ex3); |
| 2558 | if (err == -ENOSPC) { |
| 2559 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2560 | if (err) |
| 2561 | goto fix_extent_len; |
| 2562 | ex->ee_block = orig_ex.ee_block; |
| 2563 | ex->ee_len = orig_ex.ee_len; |
| 2564 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2565 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2566 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2567 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2568 | |
| 2569 | } else if (err) |
| 2570 | goto fix_extent_len; |
| 2571 | |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2572 | /* |
| 2573 | * We need to zero out the second half because |
| 2574 | * an fallocate request can update file size and |
| 2575 | * converting the second half to initialized extent |
| 2576 | * implies that we can leak some junk data to user |
| 2577 | * space. |
| 2578 | */ |
| 2579 | err = ext4_ext_zeroout(inode, ex3); |
| 2580 | if (err) { |
| 2581 | /* |
| 2582 | * We should actually mark the |
| 2583 | * second half as uninit and return error |
| 2584 | * Insert would have changed the extent |
| 2585 | */ |
| 2586 | depth = ext_depth(inode); |
| 2587 | ext4_ext_drop_refs(path); |
| 2588 | path = ext4_ext_find_extent(inode, |
| 2589 | iblock, path); |
| 2590 | if (IS_ERR(path)) { |
| 2591 | err = PTR_ERR(path); |
| 2592 | return err; |
| 2593 | } |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2594 | /* get the second half extent details */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2595 | ex = path[depth].p_ext; |
| 2596 | err = ext4_ext_get_access(handle, inode, |
| 2597 | path + depth); |
| 2598 | if (err) |
| 2599 | return err; |
| 2600 | ext4_ext_mark_uninitialized(ex); |
| 2601 | ext4_ext_dirty(handle, inode, path + depth); |
| 2602 | return err; |
| 2603 | } |
| 2604 | |
| 2605 | /* zeroed the second half */ |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2606 | return allocated; |
| 2607 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2608 | ex3 = &newex; |
| 2609 | ex3->ee_block = cpu_to_le32(iblock + max_blocks); |
| 2610 | ext4_ext_store_pblock(ex3, newblock + max_blocks); |
| 2611 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); |
| 2612 | ext4_ext_mark_uninitialized(ex3); |
| 2613 | err = ext4_ext_insert_extent(handle, inode, path, ex3); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2614 | if (err == -ENOSPC) { |
| 2615 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2616 | if (err) |
| 2617 | goto fix_extent_len; |
| 2618 | /* update the extent length and mark as initialized */ |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2619 | ex->ee_block = orig_ex.ee_block; |
| 2620 | ex->ee_len = orig_ex.ee_len; |
| 2621 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2622 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2623 | /* zeroed the full extent */ |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2624 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2625 | return allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2626 | |
| 2627 | } else if (err) |
| 2628 | goto fix_extent_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2629 | /* |
| 2630 | * The depth, and hence eh & ex might change |
| 2631 | * as part of the insert above. |
| 2632 | */ |
| 2633 | newdepth = ext_depth(inode); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2634 | /* |
Coly Li | 73ac36e | 2009-01-07 18:09:16 -0800 | [diff] [blame] | 2635 | * update the extent length after successful insert of the |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2636 | * split extent |
| 2637 | */ |
| 2638 | orig_ex.ee_len = cpu_to_le16(ee_len - |
| 2639 | ext4_ext_get_actual_len(ex3)); |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2640 | depth = newdepth; |
| 2641 | ext4_ext_drop_refs(path); |
| 2642 | path = ext4_ext_find_extent(inode, iblock, path); |
| 2643 | if (IS_ERR(path)) { |
| 2644 | err = PTR_ERR(path); |
| 2645 | goto out; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2646 | } |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2647 | eh = path[depth].p_hdr; |
| 2648 | ex = path[depth].p_ext; |
| 2649 | if (ex2 != &newex) |
| 2650 | ex2 = ex; |
| 2651 | |
| 2652 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2653 | if (err) |
| 2654 | goto out; |
| 2655 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2656 | allocated = max_blocks; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2657 | |
| 2658 | /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying |
| 2659 | * to insert a extent in the middle zerout directly |
| 2660 | * otherwise give the extent a chance to merge to left |
| 2661 | */ |
| 2662 | if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && |
| 2663 | iblock != ee_block) { |
| 2664 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2665 | if (err) |
| 2666 | goto fix_extent_len; |
| 2667 | /* update the extent length and mark as initialized */ |
| 2668 | ex->ee_block = orig_ex.ee_block; |
| 2669 | ex->ee_len = orig_ex.ee_len; |
| 2670 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2671 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2672 | /* zero out the first half */ |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2673 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2674 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2675 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2676 | } |
| 2677 | /* |
| 2678 | * If there was a change of depth as part of the |
| 2679 | * insertion of ex3 above, we need to update the length |
| 2680 | * of the ex1 extent again here |
| 2681 | */ |
| 2682 | if (ex1 && ex1 != ex) { |
| 2683 | ex1 = ex; |
| 2684 | ex1->ee_len = cpu_to_le16(iblock - ee_block); |
| 2685 | ext4_ext_mark_uninitialized(ex1); |
| 2686 | ex2 = &newex; |
| 2687 | } |
| 2688 | /* ex2: iblock to iblock + maxblocks-1 : initialised */ |
| 2689 | ex2->ee_block = cpu_to_le32(iblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2690 | ext4_ext_store_pblock(ex2, newblock); |
| 2691 | ex2->ee_len = cpu_to_le16(allocated); |
| 2692 | if (ex2 != ex) |
| 2693 | goto insert; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2694 | /* |
| 2695 | * New (initialized) extent starts from the first block |
| 2696 | * in the current extent. i.e., ex2 == ex |
| 2697 | * We have to see if it can be merged with the extent |
| 2698 | * on the left. |
| 2699 | */ |
| 2700 | if (ex2 > EXT_FIRST_EXTENT(eh)) { |
| 2701 | /* |
| 2702 | * To merge left, pass "ex2 - 1" to try_to_merge(), |
| 2703 | * since it merges towards right _only_. |
| 2704 | */ |
| 2705 | ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); |
| 2706 | if (ret) { |
| 2707 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2708 | if (err) |
| 2709 | goto out; |
| 2710 | depth = ext_depth(inode); |
| 2711 | ex2--; |
| 2712 | } |
| 2713 | } |
| 2714 | /* |
| 2715 | * Try to Merge towards right. This might be required |
| 2716 | * only when the whole extent is being written to. |
| 2717 | * i.e. ex2 == ex and ex3 == NULL. |
| 2718 | */ |
| 2719 | if (!ex3) { |
| 2720 | ret = ext4_ext_try_to_merge(inode, path, ex2); |
| 2721 | if (ret) { |
| 2722 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2723 | if (err) |
| 2724 | goto out; |
| 2725 | } |
| 2726 | } |
| 2727 | /* Mark modified extent as dirty */ |
| 2728 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2729 | goto out; |
| 2730 | insert: |
| 2731 | err = ext4_ext_insert_extent(handle, inode, path, &newex); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2732 | if (err == -ENOSPC) { |
| 2733 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2734 | if (err) |
| 2735 | goto fix_extent_len; |
| 2736 | /* update the extent length and mark as initialized */ |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2737 | ex->ee_block = orig_ex.ee_block; |
| 2738 | ex->ee_len = orig_ex.ee_len; |
| 2739 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2740 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2741 | /* zero out the first half */ |
| 2742 | return allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2743 | } else if (err) |
| 2744 | goto fix_extent_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2745 | out: |
| 2746 | return err ? err : allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2747 | |
| 2748 | fix_extent_len: |
| 2749 | ex->ee_block = orig_ex.ee_block; |
| 2750 | ex->ee_len = orig_ex.ee_len; |
| 2751 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2752 | ext4_ext_mark_uninitialized(ex); |
| 2753 | ext4_ext_dirty(handle, inode, path + depth); |
| 2754 | return err; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2755 | } |
| 2756 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2757 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 2758 | * Block allocation/map/preallocation routine for extents based files |
| 2759 | * |
| 2760 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2761 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 2762 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 2763 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 2764 | * |
| 2765 | * return > 0, number of of blocks already mapped/allocated |
| 2766 | * if create == 0 and these are pre-allocated blocks |
| 2767 | * buffer head is unmapped |
| 2768 | * otherwise blocks are mapped |
| 2769 | * |
| 2770 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 2771 | * buffer head is unmapped |
| 2772 | * |
| 2773 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2774 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2775 | int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2776 | ext4_lblk_t iblock, |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2777 | unsigned int max_blocks, struct buffer_head *bh_result, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2778 | int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2779 | { |
| 2780 | struct ext4_ext_path *path = NULL; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2781 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2782 | struct ext4_extent newex, *ex; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2783 | ext4_fsblk_t newblock; |
| 2784 | int err = 0, depth, ret, cache_type; |
| 2785 | unsigned int allocated = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2786 | struct ext4_allocation_request ar; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2787 | |
| 2788 | __clear_bit(BH_New, &bh_result->b_state); |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2789 | ext_debug("blocks %u/%u requested for inode %u\n", |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2790 | iblock, max_blocks, inode->i_ino); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2791 | |
| 2792 | /* check in cache */ |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2793 | cache_type = ext4_ext_in_cache(inode, iblock, &newex); |
| 2794 | if (cache_type) { |
| 2795 | if (cache_type == EXT4_EXT_CACHE_GAP) { |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2796 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2797 | /* |
| 2798 | * block isn't allocated yet and |
| 2799 | * user doesn't want to allocate it |
| 2800 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2801 | goto out2; |
| 2802 | } |
| 2803 | /* we should allocate requested block */ |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2804 | } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2805 | /* block is already allocated */ |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2806 | newblock = iblock |
| 2807 | - le32_to_cpu(newex.ee_block) |
| 2808 | + ext_pblock(&newex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2809 | /* number of remaining blocks in the extent */ |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2810 | allocated = ext4_ext_get_actual_len(&newex) - |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2811 | (iblock - le32_to_cpu(newex.ee_block)); |
| 2812 | goto out; |
| 2813 | } else { |
| 2814 | BUG(); |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | /* find extent for this block */ |
| 2819 | path = ext4_ext_find_extent(inode, iblock, NULL); |
| 2820 | if (IS_ERR(path)) { |
| 2821 | err = PTR_ERR(path); |
| 2822 | path = NULL; |
| 2823 | goto out2; |
| 2824 | } |
| 2825 | |
| 2826 | depth = ext_depth(inode); |
| 2827 | |
| 2828 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2829 | * consistent leaf must not be empty; |
| 2830 | * this situation is possible, though, _during_ tree modification; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2831 | * this is why assert can't be put in ext4_ext_find_extent() |
| 2832 | */ |
| 2833 | BUG_ON(path[depth].p_ext == NULL && depth != 0); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2834 | eh = path[depth].p_hdr; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2835 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2836 | ex = path[depth].p_ext; |
| 2837 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2838 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2839 | ext4_fsblk_t ee_start = ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2840 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2841 | |
| 2842 | /* |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2843 | * Uninitialized extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2844 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2845 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2846 | ee_len = ext4_ext_get_actual_len(ex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2847 | /* if found extent covers block, simply return it */ |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2848 | if (iblock >= ee_block && iblock < ee_block + ee_len) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2849 | newblock = iblock - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2850 | /* number of remaining blocks in the extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2851 | allocated = ee_len - (iblock - ee_block); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2852 | ext_debug("%u fit into %lu:%d -> %llu\n", iblock, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2853 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2854 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2855 | /* Do not put uninitialized extent in the cache */ |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2856 | if (!ext4_ext_is_uninitialized(ex)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2857 | ext4_ext_put_in_cache(inode, ee_block, |
| 2858 | ee_len, ee_start, |
| 2859 | EXT4_EXT_CACHE_EXTENT); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2860 | goto out; |
| 2861 | } |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2862 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2863 | goto out; |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2864 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Aneesh Kumar K.V | 29fa89d | 2009-05-12 16:30:27 -0400 | [diff] [blame] | 2865 | if (allocated > max_blocks) |
| 2866 | allocated = max_blocks; |
Aneesh Kumar K.V | e067ba0 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2867 | /* |
| 2868 | * We have blocks reserved already. We |
| 2869 | * return allocated blocks so that delalloc |
| 2870 | * won't do block reservation for us. But |
| 2871 | * the buffer head will be unmapped so that |
| 2872 | * a read from the block returns 0s. |
| 2873 | */ |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2874 | set_buffer_unwritten(bh_result); |
Aneesh Kumar K.V | 9c1ee18 | 2009-05-13 18:36:58 -0400 | [diff] [blame] | 2875 | bh_result->b_bdev = inode->i_sb->s_bdev; |
| 2876 | bh_result->b_blocknr = newblock; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2877 | goto out2; |
Aneesh Kumar K.V | e067ba0 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2878 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2879 | |
| 2880 | ret = ext4_ext_convert_to_initialized(handle, inode, |
| 2881 | path, iblock, |
| 2882 | max_blocks); |
Dmitry Monakhov | dbf9d7d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2883 | if (ret <= 0) { |
| 2884 | err = ret; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2885 | goto out2; |
Dmitry Monakhov | dbf9d7d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2886 | } else |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2887 | allocated = ret; |
| 2888 | goto outnew; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2893 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2894 | * we couldn't try to create block if create flag is zero |
| 2895 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2896 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2897 | /* |
| 2898 | * put just found gap into cache to speed up |
| 2899 | * subsequent requests |
| 2900 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2901 | ext4_ext_put_gap_in_cache(inode, path, iblock); |
| 2902 | goto out2; |
| 2903 | } |
| 2904 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 2905 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 2906 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2907 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2908 | /* find neighbour allocated blocks */ |
| 2909 | ar.lleft = iblock; |
| 2910 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 2911 | if (err) |
| 2912 | goto out2; |
| 2913 | ar.lright = iblock; |
| 2914 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); |
| 2915 | if (err) |
| 2916 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2917 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2918 | /* |
| 2919 | * See if request is beyond maximum number of blocks we can have in |
| 2920 | * a single extent. For an initialized extent this limit is |
| 2921 | * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is |
| 2922 | * EXT_UNINIT_MAX_LEN. |
| 2923 | */ |
| 2924 | if (max_blocks > EXT_INIT_MAX_LEN && |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2925 | !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2926 | max_blocks = EXT_INIT_MAX_LEN; |
| 2927 | else if (max_blocks > EXT_UNINIT_MAX_LEN && |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2928 | (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2929 | max_blocks = EXT_UNINIT_MAX_LEN; |
| 2930 | |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2931 | /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ |
| 2932 | newex.ee_block = cpu_to_le32(iblock); |
| 2933 | newex.ee_len = cpu_to_le16(max_blocks); |
| 2934 | err = ext4_ext_check_overlap(inode, &newex, path); |
| 2935 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2936 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2937 | else |
| 2938 | allocated = max_blocks; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2939 | |
| 2940 | /* allocate new block */ |
| 2941 | ar.inode = inode; |
| 2942 | ar.goal = ext4_ext_find_goal(inode, path, iblock); |
| 2943 | ar.logical = iblock; |
| 2944 | ar.len = allocated; |
| 2945 | if (S_ISREG(inode->i_mode)) |
| 2946 | ar.flags = EXT4_MB_HINT_DATA; |
| 2947 | else |
| 2948 | /* disable in-core preallocation for non-regular files */ |
| 2949 | ar.flags = 0; |
| 2950 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2951 | if (!newblock) |
| 2952 | goto out2; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2953 | ext_debug("allocate new block: goal %llu, found %llu/%lu\n", |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2954 | ar.goal, newblock, allocated); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2955 | |
| 2956 | /* try to insert new extent into found leaf and return */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2957 | ext4_ext_store_pblock(&newex, newblock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2958 | newex.ee_len = cpu_to_le16(ar.len); |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2959 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) /* Mark uninitialized */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2960 | ext4_ext_mark_uninitialized(&newex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2961 | err = ext4_ext_insert_extent(handle, inode, path, &newex); |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2962 | if (err) { |
| 2963 | /* free data blocks we just allocated */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2964 | /* not a good idea to call discard here directly, |
| 2965 | * but otherwise we'd need to call it every free() */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 2966 | ext4_discard_preallocations(inode); |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2967 | ext4_free_blocks(handle, inode, ext_pblock(&newex), |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2968 | ext4_ext_get_actual_len(&newex), 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2969 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2970 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2971 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2972 | /* previous routine could use block we allocated */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2973 | newblock = ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2974 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2975 | outnew: |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2976 | set_buffer_new(bh_result); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2977 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2978 | /* Cache only when it is _not_ an uninitialized extent */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 2979 | if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2980 | ext4_ext_put_in_cache(inode, iblock, allocated, newblock, |
| 2981 | EXT4_EXT_CACHE_EXTENT); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2982 | out: |
| 2983 | if (allocated > max_blocks) |
| 2984 | allocated = max_blocks; |
| 2985 | ext4_ext_show_leaf(inode, path); |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2986 | set_buffer_mapped(bh_result); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2987 | bh_result->b_bdev = inode->i_sb->s_bdev; |
| 2988 | bh_result->b_blocknr = newblock; |
| 2989 | out2: |
| 2990 | if (path) { |
| 2991 | ext4_ext_drop_refs(path); |
| 2992 | kfree(path); |
| 2993 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2994 | return err ? err : allocated; |
| 2995 | } |
| 2996 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2997 | void ext4_ext_truncate(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2998 | { |
| 2999 | struct address_space *mapping = inode->i_mapping; |
| 3000 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3001 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3002 | handle_t *handle; |
| 3003 | int err = 0; |
| 3004 | |
| 3005 | /* |
| 3006 | * probably first extent we're gonna free will be last in block |
| 3007 | */ |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 3008 | err = ext4_writepage_trans_blocks(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3009 | handle = ext4_journal_start(inode, err); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3010 | if (IS_ERR(handle)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3011 | return; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3012 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3013 | if (inode->i_size & (sb->s_blocksize - 1)) |
| 3014 | ext4_block_truncate_page(handle, mapping, inode->i_size); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3015 | |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3016 | if (ext4_orphan_add(handle, inode)) |
| 3017 | goto out_stop; |
| 3018 | |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3019 | down_write(&EXT4_I(inode)->i_data_sem); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3020 | ext4_ext_invalidate_cache(inode); |
| 3021 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3022 | ext4_discard_preallocations(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3023 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3024 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3025 | * TODO: optimization is possible here. |
| 3026 | * Probably we need not scan at all, |
| 3027 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3028 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3029 | |
| 3030 | /* we have to know where to truncate from in crash case */ |
| 3031 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 3032 | ext4_mark_inode_dirty(handle, inode); |
| 3033 | |
| 3034 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 3035 | >> EXT4_BLOCK_SIZE_BITS(sb); |
| 3036 | err = ext4_ext_remove_space(inode, last_block); |
| 3037 | |
| 3038 | /* In a multi-transaction truncate, we only make the final |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3039 | * transaction synchronous. |
| 3040 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3041 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3042 | ext4_handle_sync(handle); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3043 | |
| 3044 | out_stop: |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3045 | up_write(&EXT4_I(inode)->i_data_sem); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3046 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3047 | * If this was a simple ftruncate() and the file will remain alive, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3048 | * then we need to clear up the orphan record which we created above. |
| 3049 | * However, if this was a real unlink then we were called by |
| 3050 | * ext4_delete_inode(), and we allow that function to clean up the |
| 3051 | * orphan info for us. |
| 3052 | */ |
| 3053 | if (inode->i_nlink) |
| 3054 | ext4_orphan_del(handle, inode); |
| 3055 | |
Solofo Ramangalahy | ef73772 | 2008-04-29 22:00:41 -0400 | [diff] [blame] | 3056 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 3057 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3058 | ext4_journal_stop(handle); |
| 3059 | } |
| 3060 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3061 | static void ext4_falloc_update_inode(struct inode *inode, |
| 3062 | int mode, loff_t new_size, int update_ctime) |
| 3063 | { |
| 3064 | struct timespec now; |
| 3065 | |
| 3066 | if (update_ctime) { |
| 3067 | now = current_fs_time(inode->i_sb); |
| 3068 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 3069 | inode->i_ctime = now; |
| 3070 | } |
| 3071 | /* |
| 3072 | * Update only when preallocation was requested beyond |
| 3073 | * the file size. |
| 3074 | */ |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 3075 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
| 3076 | if (new_size > i_size_read(inode)) |
| 3077 | i_size_write(inode, new_size); |
| 3078 | if (new_size > EXT4_I(inode)->i_disksize) |
| 3079 | ext4_update_i_disksize(inode, new_size); |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | } |
| 3083 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3084 | /* |
| 3085 | * preallocate space for a file. This implements ext4's fallocate inode |
| 3086 | * operation, which gets called from sys_fallocate system call. |
| 3087 | * For block-mapped files, posix_fallocate should fall back to the method |
| 3088 | * of writing zeroes to the required new blocks (the same behavior which is |
| 3089 | * expected for file systems which do not support fallocate() system call). |
| 3090 | */ |
| 3091 | long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) |
| 3092 | { |
| 3093 | handle_t *handle; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3094 | ext4_lblk_t block; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3095 | loff_t new_size; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3096 | unsigned int max_blocks; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3097 | int ret = 0; |
| 3098 | int ret2 = 0; |
| 3099 | int retries = 0; |
| 3100 | struct buffer_head map_bh; |
| 3101 | unsigned int credits, blkbits = inode->i_blkbits; |
| 3102 | |
| 3103 | /* |
| 3104 | * currently supporting (pre)allocate mode for extent-based |
| 3105 | * files _only_ |
| 3106 | */ |
| 3107 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) |
| 3108 | return -EOPNOTSUPP; |
| 3109 | |
| 3110 | /* preallocation to directories is currently not supported */ |
| 3111 | if (S_ISDIR(inode->i_mode)) |
| 3112 | return -ENODEV; |
| 3113 | |
| 3114 | block = offset >> blkbits; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3115 | /* |
| 3116 | * We can't just convert len to max_blocks because |
| 3117 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 3118 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3119 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3120 | - block; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3121 | /* |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 3122 | * credits to insert 1 extent into extent tree |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3123 | */ |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 3124 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 3125 | mutex_lock(&inode->i_mutex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3126 | retry: |
| 3127 | while (ret >= 0 && ret < max_blocks) { |
| 3128 | block = block + ret; |
| 3129 | max_blocks = max_blocks - ret; |
| 3130 | handle = ext4_journal_start(inode, credits); |
| 3131 | if (IS_ERR(handle)) { |
| 3132 | ret = PTR_ERR(handle); |
| 3133 | break; |
| 3134 | } |
Aneesh Kumar K.V | 79ffab3 | 2009-05-13 15:13:42 -0400 | [diff] [blame] | 3135 | map_bh.b_state = 0; |
Theodore Ts'o | 12b7ac1 | 2009-05-14 00:57:44 -0400 | [diff] [blame] | 3136 | ret = ext4_get_blocks(handle, inode, block, |
| 3137 | max_blocks, &map_bh, |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3138 | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3139 | if (ret <= 0) { |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 3140 | #ifdef EXT4FS_DEBUG |
| 3141 | WARN_ON(ret <= 0); |
| 3142 | printk(KERN_ERR "%s: ext4_ext_get_blocks " |
| 3143 | "returned error inode#%lu, block=%u, " |
Thadeu Lima de Souza Cascardo | 9fd9784 | 2009-01-26 19:26:26 -0500 | [diff] [blame] | 3144 | "max_blocks=%u", __func__, |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3145 | inode->i_ino, block, max_blocks); |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 3146 | #endif |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3147 | ext4_mark_inode_dirty(handle, inode); |
| 3148 | ret2 = ext4_journal_stop(handle); |
| 3149 | break; |
| 3150 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3151 | if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, |
| 3152 | blkbits) >> blkbits)) |
| 3153 | new_size = offset + len; |
| 3154 | else |
| 3155 | new_size = (block + ret) << blkbits; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3156 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3157 | ext4_falloc_update_inode(inode, mode, new_size, |
| 3158 | buffer_new(&map_bh)); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3159 | ext4_mark_inode_dirty(handle, inode); |
| 3160 | ret2 = ext4_journal_stop(handle); |
| 3161 | if (ret2) |
| 3162 | break; |
| 3163 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3164 | if (ret == -ENOSPC && |
| 3165 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 3166 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3167 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3168 | } |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 3169 | mutex_unlock(&inode->i_mutex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3170 | return ret > 0 ? ret2 : ret; |
| 3171 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3172 | |
| 3173 | /* |
| 3174 | * Callback function called for each extent to gather FIEMAP information. |
| 3175 | */ |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 3176 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3177 | struct ext4_ext_cache *newex, struct ext4_extent *ex, |
| 3178 | void *data) |
| 3179 | { |
| 3180 | struct fiemap_extent_info *fieinfo = data; |
Eric Sandeen | c9877b2 | 2009-05-01 23:32:06 -0400 | [diff] [blame] | 3181 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3182 | __u64 logical; |
| 3183 | __u64 physical; |
| 3184 | __u64 length; |
| 3185 | __u32 flags = 0; |
| 3186 | int error; |
| 3187 | |
| 3188 | logical = (__u64)newex->ec_block << blksize_bits; |
| 3189 | |
| 3190 | if (newex->ec_type == EXT4_EXT_CACHE_GAP) { |
| 3191 | pgoff_t offset; |
| 3192 | struct page *page; |
| 3193 | struct buffer_head *bh = NULL; |
| 3194 | |
| 3195 | offset = logical >> PAGE_SHIFT; |
| 3196 | page = find_get_page(inode->i_mapping, offset); |
| 3197 | if (!page || !page_has_buffers(page)) |
| 3198 | return EXT_CONTINUE; |
| 3199 | |
| 3200 | bh = page_buffers(page); |
| 3201 | |
| 3202 | if (!bh) |
| 3203 | return EXT_CONTINUE; |
| 3204 | |
| 3205 | if (buffer_delay(bh)) { |
| 3206 | flags |= FIEMAP_EXTENT_DELALLOC; |
| 3207 | page_cache_release(page); |
| 3208 | } else { |
| 3209 | page_cache_release(page); |
| 3210 | return EXT_CONTINUE; |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | physical = (__u64)newex->ec_start << blksize_bits; |
| 3215 | length = (__u64)newex->ec_len << blksize_bits; |
| 3216 | |
| 3217 | if (ex && ext4_ext_is_uninitialized(ex)) |
| 3218 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 3219 | |
| 3220 | /* |
| 3221 | * If this extent reaches EXT_MAX_BLOCK, it must be last. |
| 3222 | * |
| 3223 | * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, |
| 3224 | * this also indicates no more allocated blocks. |
| 3225 | * |
| 3226 | * XXX this might miss a single-block extent at EXT_MAX_BLOCK |
| 3227 | */ |
Eric Sandeen | c9877b2 | 2009-05-01 23:32:06 -0400 | [diff] [blame] | 3228 | if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK || |
Theodore Ts'o | eefd7f0 | 2009-05-02 19:05:37 -0400 | [diff] [blame] | 3229 | newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) { |
| 3230 | loff_t size = i_size_read(inode); |
| 3231 | loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb); |
| 3232 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3233 | flags |= FIEMAP_EXTENT_LAST; |
Theodore Ts'o | eefd7f0 | 2009-05-02 19:05:37 -0400 | [diff] [blame] | 3234 | if ((flags & FIEMAP_EXTENT_DELALLOC) && |
| 3235 | logical+length > size) |
| 3236 | length = (size - logical + bs - 1) & ~(bs-1); |
| 3237 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3238 | |
| 3239 | error = fiemap_fill_next_extent(fieinfo, logical, physical, |
| 3240 | length, flags); |
| 3241 | if (error < 0) |
| 3242 | return error; |
| 3243 | if (error == 1) |
| 3244 | return EXT_BREAK; |
| 3245 | |
| 3246 | return EXT_CONTINUE; |
| 3247 | } |
| 3248 | |
| 3249 | /* fiemap flags we can handle specified here */ |
| 3250 | #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 3251 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 3252 | static int ext4_xattr_fiemap(struct inode *inode, |
| 3253 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3254 | { |
| 3255 | __u64 physical = 0; |
| 3256 | __u64 length; |
| 3257 | __u32 flags = FIEMAP_EXTENT_LAST; |
| 3258 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 3259 | int error = 0; |
| 3260 | |
| 3261 | /* in-inode? */ |
| 3262 | if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) { |
| 3263 | struct ext4_iloc iloc; |
| 3264 | int offset; /* offset of xattr in inode */ |
| 3265 | |
| 3266 | error = ext4_get_inode_loc(inode, &iloc); |
| 3267 | if (error) |
| 3268 | return error; |
| 3269 | physical = iloc.bh->b_blocknr << blockbits; |
| 3270 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 3271 | EXT4_I(inode)->i_extra_isize; |
| 3272 | physical += offset; |
| 3273 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
| 3274 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
| 3275 | } else { /* external block */ |
| 3276 | physical = EXT4_I(inode)->i_file_acl << blockbits; |
| 3277 | length = inode->i_sb->s_blocksize; |
| 3278 | } |
| 3279 | |
| 3280 | if (physical) |
| 3281 | error = fiemap_fill_next_extent(fieinfo, 0, physical, |
| 3282 | length, flags); |
| 3283 | return (error < 0 ? error : 0); |
| 3284 | } |
| 3285 | |
| 3286 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 3287 | __u64 start, __u64 len) |
| 3288 | { |
| 3289 | ext4_lblk_t start_blk; |
| 3290 | ext4_lblk_t len_blks; |
| 3291 | int error = 0; |
| 3292 | |
| 3293 | /* fallback to generic here if not in extents fmt */ |
| 3294 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) |
| 3295 | return generic_block_fiemap(inode, fieinfo, start, len, |
| 3296 | ext4_get_block); |
| 3297 | |
| 3298 | if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) |
| 3299 | return -EBADR; |
| 3300 | |
| 3301 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 3302 | error = ext4_xattr_fiemap(inode, fieinfo); |
| 3303 | } else { |
| 3304 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
| 3305 | len_blks = len >> inode->i_sb->s_blocksize_bits; |
| 3306 | |
| 3307 | /* |
| 3308 | * Walk the extent tree gathering extent information. |
| 3309 | * ext4_ext_fiemap_cb will push extents back to user. |
| 3310 | */ |
Theodore Ts'o | 0568c51 | 2009-05-17 23:31:23 -0400 | [diff] [blame] | 3311 | down_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3312 | error = ext4_ext_walk_space(inode, start_blk, len_blks, |
| 3313 | ext4_ext_fiemap_cb, fieinfo); |
Theodore Ts'o | 0568c51 | 2009-05-17 23:31:23 -0400 | [diff] [blame] | 3314 | up_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | return error; |
| 3318 | } |
| 3319 | |