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